需要更改图表matplotlib中一个轴的速度

2024-05-03 04:36:43 发布

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

对绘图相当陌生,需要帮助 有我正在做的这个DATAFRAME。试图将左侧y_轴上的速度更改为5而不是10,但我猜“AxeSubplot”对象没有属性“yticks”,因此无法使用

ax.yticks(np.arange(0, max(GDP['GDP'])+5, 5))

这是我的代码(绘图部分)

fig = plt.figure() # Create matplotlib figure

ax = fig.add_subplot(111) # Create matplotlib axes
ax2 = ax.twinx() # Create another axes that shares the same x-axis as ax.

width = 0.7
newdf.GDP.plot(kind='bar', color='#8D99FB', ax=ax, width=width)
newdf.Unemployment.plot(kind='line', color='red', ax=ax2)

ax.set_ylabel('GDP over The years in billions of dollars')
#ax.yticks(np.arange(0, max(GDP['GDP'])+5, 5))
ax2.set_ylabel('Percentage of Unemployment/total Population')
ax.set_xticklabels(newdf.Year.tolist())

ax.set_xlim(-width,len(newdf.Year.tolist()))

plt.title('GDP over The years in billions of dollars')



'''
ax.yticks(np.arange(0, max(GDP['GDP'])+5, 5))

'''
plt.show()

谢谢