如何使Matplotlib显示所有tick标签?
我正在尝试绘制一些时间序列,并在tick标签上挣扎。 我的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)
绘图看起来像
https:/https:/ /I.SSTATIC.NET/TKQP6.png
我该如何使其全年显示为X tick?
先感谢您
I am trying to plot some time series' and I struggle with the tick labels.
my df looks like this:
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
The code to plot it looks like this:
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)
The plot however looks like this
https://i.sstatic.net/tKqP6.png
How can I make it thath it shows all years as x ticks?
Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下操作:
输出是:
我使用
set_xticklabels()
函数,而不是tick_params()
。我发现此教程在这里。您必须将其列出您想要的刻度标记列表。如果您只想查看感兴趣的图:
输出:
Try this:
The output is:
I used
set_xticklabels()
function instead oftick_params()
. I found this tutorial here. You have to give it the list of tick marks that you desire.If you want to see only the plot of interest:
The output:
Then I personally like to modify tick marks using the function
plt.setp()
:The output is: