[英]Matplotlib Rotate xticklabels using ax.set()
這是我一段時間以來一直感興趣的東西。 顯然,我們可以創建一個matplotlib圖並旋轉xticklabel這樣的。
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(np.random.randn(1000).cumsum())
ax.set_title('Random Cumulative Sum')
ax.set_xlabel('Stages')
ax.set_xticks([0, 250, 500, 750, 1000])
ax.set_xticklabels(['one','two','three','four','five'],
rotation=30, fontsize='small')
假設我寧願使用字典來完成此操作。 我可以使用(幾乎)相同的情節
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(np.random.randn(1000).cumsum())
props = {'title': 'Random Cumulative Sum',
'xlabel': 'stages',
'xticks': [0,250,500,750,1000],
'xticklabels': ['one','two','three','four','five']}
ax.set(**props)
我沒有指定需要在props字典中旋轉xticklabels。 有沒有辦法在字典中包含此信息?
旋轉是刻度標簽的屬性,而不是軸的屬性。 您可以使用plt.setp
設置任何對象的屬性。
props = {"rotation" : 30}
plt.setp(ax.get_xticklabels(), **props)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.