如何使Matplotlib显示所有tick标签?

发布于 2025-01-24 03:00:47 字数 859 浏览 0 评论 0原文

我正在尝试绘制一些时间序列,并在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

快乐很简单 2025-01-31 03:00:48

尝试以下操作:

import pandas as pd
import matplotlib.pylab as plt

data = pd.read_csv("years_data.txt", sep="    ")

fig, (ax1, ax2,ax3, ax4) = plt.subplots(4, 1, figsize = (20,20))
data.plot(x='Year', y='Q1-Q5', ax = ax1)
ax1.set_xticklabels(data['Year'])
ax1.grid(axis = 'y')
ax1.set_ylim(-1.5, 1.5)

输出是:

我使用set_xticklabels()函数,而不是tick_params()。我发现此教程在这里。您必须将其列出您想要的刻度标记列表。

如果您只想查看感兴趣的图:

fig, ax =plt.subplots()
data.plot(x='Year', y='Q1-Q5', ax =ax)
ax.set_xticklabels(data['Year'])
ax.set_ylim(-1.5, 1.5)

输出:

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)

Try this:

import pandas as pd
import matplotlib.pylab as plt

data = pd.read_csv("years_data.txt", sep="    ")

fig, (ax1, ax2,ax3, ax4) = plt.subplots(4, 1, figsize = (20,20))
data.plot(x='Year', y='Q1-Q5', ax = ax1)
ax1.set_xticklabels(data['Year'])
ax1.grid(axis = 'y')
ax1.set_ylim(-1.5, 1.5)

The output is:
enter image description here

I used set_xticklabels() function instead of tick_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:

fig, ax =plt.subplots()
data.plot(x='Year', y='Q1-Q5', ax =ax)
ax.set_xticklabels(data['Year'])
ax.set_ylim(-1.5, 1.5)

The output:

enter image description here

Then I personally like to modify tick marks using the function 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)

The output is:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文