Matplotlib:个性化x轴

问题描述 投票:1回答:1

我有计算结果,我正在尝试使用个性化的x轴对其进行绘制。

import numpy as np
import math
import matplotlib.pyplot as plt

maxPar = 100
minPar = 6
step = 1
parameterList = np.arange(minPar, maxPar, step)
footprint = np.ones(parameterList.size)

for parameter in parameterList:
    pl = 20*math.log10(parameter) + 28.0 + 22*math.log10(math.sqrt(200**2 + math.fabs(8.5)**2))
    footprint[ np.where(parameterList == parameter)[0][0] ] = 30+25+2.15 - pl

plt.plot(parameterList, footprint)
plt.xticks(np.arange(min(parameterList), max(parameterList), 4.0))
plt.margins(0, x=True)
plt.show()

plot

我的目标是使x轴的标度为10-20-30 -....- 90-100,但我想一开始使用6。如果不可能的话,我至少要在图的开始处添加6,在图的末尾添加100。

python numpy matplotlib axis axis-labels
1个回答
0
投票

查看此,

import numpy as np
import math
import matplotlib.pyplot as plt

maxPar = 100
minPar = 6
step = 1
parameterList = np.arange(minPar, maxPar, step)
footprint = np.ones(parameterList.size)

for parameter in parameterList:
    pl = 20*math.log10(parameter) + 28.0 + 22*math.log10(math.sqrt(200**2 + math.fabs(8.5)**2))
    footprint[ np.where(parameterList == parameter)[0][0] ] = 30+25+2.15 - pl

plt.plot(parameterList, footprint)
plt.xticks(np.arange(min(parameterList), max(parameterList), 4.0))
plt.margins(0, x=True)
plt.xticks([6] + list(np.arange(10,110,10)))    ##### Add this line ####
plt.show()

输出:

enter image description here

您可以在此处了解有关matplotlib使用的更多信息,https://matplotlib.org/api/pyplot_api.html

更新:

import numpy as np
import math
import matplotlib.pyplot as plt

maxPar = 100
minPar = 6
step = 1
parameterList = np.arange(minPar, maxPar, step)
footprint = np.ones(parameterList.size)

for parameter in parameterList:
    pl = 20*math.log10(parameter) + 28.0 + 22*math.log10(math.sqrt(200**2 + math.fabs(8.5)**2))
    footprint[ np.where(parameterList == parameter)[0][0] ] = 30+25+2.15 - pl

plt.plot(parameterList, footprint)
plt.margins(0, x=True)
x = [0.6] + list(np.arange(10,110,10))
plt.xticks( x , [str(i) for i in x])

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.