我正在嘗試繪制一些時間序列'并且我與刻度標簽作斗爭。我的 df 看起來像這樣:
Q1-Q5
Date
2003 -0.183333
2004 -0.195833
2005 0.044167
2006 -0.040000
2007 0.841667
2008 0.251667
2009 -0.913333
2010 -0.471667
2011 0.005833
2012 -0.297500
2013 -0.625833
2014 0.290833
2015 0.059167
2016 0.632500
2017 1.015000
2018 0.258333
2019 0.030000
2020 0.651667
2021 0.255000
繪制它的代碼如下所示:
fig, (ax1, ax2,ax3, ax4) = plt.subplots(4, 1, figsize = (20,20))
df.plot(ax = ax1)
ax1.set_yticks(y_ticks)
ax1.tick_params(axis='x', labelrotation = 90)
ax1.grid(axis = 'y')
ax1.set_ylim(-1.5, 1.5)
然而,情節看起來像這樣

我使用set_xticklabels()函式而不是tick_params(). 
然后我個人喜歡使用該函式修改刻度線plt.setp():
fig, ax =plt.subplots()
data.plot(x='Year', y='Q1-Q5', ax =ax)
ax.set_xticklabels(data['Year'])
plt.setp( ax.xaxis.get_majorticklabels(), rotation=-45, ha="left", weight="bold")
ax.set_ylim(-1.5, 1.5)
輸出是:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/469404.html
標籤:Python 熊猫 日期 matplotlib 阴谋
