Matplotlib:个性化xaxis

2024-09-30 04:36:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个计算的结果,我正试图用一个个性化的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

我的目标是有一个10-20-30-…-90-100的x轴,但我希望在开始时有6。如果不可能的话,我至少希望在情节的开头有6个,结尾有100个


Tags: importparameterasstepnppltmathpl
1条回答
网友
1楼 · 发布于 2024-09-30 04:36:56

检查此解决方案

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

相关问题 更多 >

    热门问题