如何使用Python库matplotlib.pyplot设置Y轴上的值间距

n9vozmp4  于 2023-01-09  发布在  Python
关注(0)|答案(1)|浏览(238)

enter image description here我尝试使用python在powerBi上绘制一个条形图,我希望y轴上的值在间隔10之后,例如0- 10、10 -20直到100。但实际上,它显示在间隔25之后,例如0-25、26-50等

import matplotlib.pyplot as plt

ax=plt.gca()

#dataset.plot(kind='bar',x='Day',y='Feed_Req_to_Standard',ax=ax) 

dataset.plot(kind='bar',x='Day',y=['Feed_%Diff_From_Stand','Feed_Req_to_Standard'], color=['red','green'],ax=ax)

plt.ylim(bottom= -100, top= 100)
plt.xlabel('Days')

plt.show()

enter image description here

nbysray5

nbysray51#

你可以使用matplotlib.axes.Axes.set_yticks()函数来设置y轴上的刻度位置和标签。如果你想仔细看看,here is the documentation。应用到你的问题上,它可能会像这样应用(注意,我还没有测试它,因为我没有访问你的数据)。

import matplotlib.pyplot as plt
from matplotlib.axes import Axes

spacing = 10
ticks = [spacing*i for i in range(5)] # gets ticks values
Axes.set_yticks(ticks)
ax=plt.gca()

dataset.plot(kind='bar',x='Day',y=['Feed_%Diff_From_Stand','Feed_Req_to_Standard'], color=['red','green'],ax=ax)

plt.ylim(bottom= -100, top= 100)
plt.xlabel('Days')

plt.show()

相关问题