我只是想爲pyplot
圖的x軸刻度標籤設置不同的旋轉角度。matplotlib.pyplot中刻度標籤的不同旋轉
讓我們藉此爲例:
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(0,5)
y=np.arange(0,5)
plt.xlim(0,4)
plt.ylim(0,4)
plt.plot(x,y)
plt.plot((1,1),(0,4),color='red',lw=1)
plt.plot((2,2),(0,4),color='red',lw=1)
plt.plot((2.98,2.98),(0,4),color='red',lw=1)
plt.plot((3,3),(0,4),color='red',lw=1)
lab_ticks=['Label 1','Label 2','Label 3','Label 4']
x_ticks=[1,2,2.98,3]
plt.xticks(x_ticks,lab_ticks,rotation=90)
plt.savefig('im1.png')
plt.show()
此代碼給我們如下圖所示:
我的問題是不是整個標籤不顯示,我知道如何解決它。我的問題是Label 3
和Label 4
附近太多,它們相互重疊。
我想設置的Label 3
在45
的rotation
和其他人在90
,但是當我試圖分開我lab_ticks
和x_ticks
,只有最後plt.xticks()
所示:
lab_ticks=['Label 1','Label 2','Label 4']
x_ticks=[1,2,3]
lab_ticks2=['Label 3']
x_ticks2=[2.98]
plt.xticks(x_ticks,lab_ticks,rotation=90)
plt.xticks(x_ticks2,lab_ticks2,rotation=45,ha='right')
有沒有人有一個技巧來解決這個問題?
在此先感謝!
Smich
即使你這樣做,他們仍然會重疊 – DavidG
@DavidG我知道你的意思,我編輯的職位。我在第二個'xticks'中添加了'ha ='right'',以便將'Label 3'正確地與其關聯的值進行匹配。 – Smich7
也許它會繼續重疊一點,但我可以弄明白。我只是想找到一個想法來設置不同的刻度標籤旋轉。 – Smich7