matplotlib Python中的X标注旋转问题

zqdjd7g9  于 2023-03-09  发布在  Python
关注(0)|答案(1)|浏览(141)

我正在做一个Python matplotlib的练习,我想把x标签旋转45度,所以我看了下面的代码,但是它仍然是水平显示x标签。

df_final = pd.DataFrame({'Count':[8601,188497,808,7081,684,15601,75,12325],
                         'Average': [0.128,0.131,0.144,.184,.134,.152,0.139,0.127]})
width = 0.8
df_final['Count'].plot(kind='bar', width = width)
df_final['Average'].plot(secondary_y=True)
ax = plt.gca()
ax.set_xticklabels(['One', 'Two', 'Three', 'Four', 'Five','Six','Seven','Eight'],rotation = 45)
plt.show()

请就这个问题提出意见。

ne5o7dgx

ne5o7dgx1#

只需更改ax = plt.gca()的位置,您可以尝试以下代码:

df_final = pd.DataFrame({'Count':[8601,188497,808,7081,684,15601,75,12325],
                         'Average': [0.128,0.131,0.144,.184,.134,.152,0.139,0.127]})
width = 0.8
ax = plt.gca()
df_final['Count'].plot(kind='bar', width = width)
df_final['Average'].plot(secondary_y=True)
ax.set_xticklabels(['One', 'Two', 'Three', 'Four', 'Five','Six','Seven','Eight'],rotation = 45)
plt.show()

相关问题