在matplotlib中更频繁地设置x轴标签名称

发布于 2025-01-21 23:17:15 字数 891 浏览 3 评论 0原文

我有一个类似的数据框架,

    Time(DDHHMM)    True AOG    Predicted AOG
5184    28:0:0.0    4.0         4.697324
5185    28:0:5.0    10.0        0.366948
5186    28:0:10.0   6.0         6.158011
5187    28:0:15.0   9.0         8.388459
5188    28:0:20.0   24.0        23.883515
... ... ... ...

5759    29:23:55.0  8.0 8.124675

我试图使用以下代码绘制它们

dataframe.plot(x='Time(DDHHMM)', y=['True','Predicted'],kind="line", figsize=(14,7),marker='.',grid=True)
plt.title("true and predicted data (EB-WB)")
plt.xlabel('Time',labelpad=2)
plt.ylabel('AOG')
plt.legend(loc='best',fancybox=True)
plt.grid(True)
plt.xlim(0,288)
plt.ylim(0,80)
plt.show() 

,并获取只有3个X轴值名称的位置。

但是,我想添加更多X轴值名,但在文档中找不到任何可行的东西。我尝试使用XTICKS(启动,结束,步骤),但没有更改任何内容。另外,我尝试了plot.locator_params(axis ='x',nbins = 20)从stackoverflow问题之一中也无法正常工作。我该如何解决?

I have a dataframe like this

    Time(DDHHMM)    True AOG    Predicted AOG
5184    28:0:0.0    4.0         4.697324
5185    28:0:5.0    10.0        0.366948
5186    28:0:10.0   6.0         6.158011
5187    28:0:15.0   9.0         8.388459
5188    28:0:20.0   24.0        23.883515
... ... ... ...

5759    29:23:55.0  8.0 8.124675

I am trying to plot them using following code

dataframe.plot(x='Time(DDHHMM)', y=['True','Predicted'],kind="line", figsize=(14,7),marker='.',grid=True)
plt.title("true and predicted data (EB-WB)")
plt.xlabel('Time',labelpad=2)
plt.ylabel('AOG')
plt.legend(loc='best',fancybox=True)
plt.grid(True)
plt.xlim(0,288)
plt.ylim(0,80)
plt.show() 

And getting figure where there are only 3 x-axis value names.

However, I want to add more x-axis value names but did not find anything in the documentation that would work. I tried using xticks(start,end,step) but it doesnt change anything. Also I tried plot.locator_params(axis='x', nbins=20) from one of the stackoverflow issues which is not working too. How could I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

岁吢 2025-01-28 23:17:15

plt.show()之前,请尝试插入此代码:

# change n to satisfy your needs
n = 3
ticks = [t for t in range(len(df)) if t % n == 0]
labels = [l for i, l in enumerate(df["Time(DDHHMM)"]) if i % n == 0]
plt.xticks(ticks, labels)

Before plt.show(), try inserting this code:

# change n to satisfy your needs
n = 3
ticks = [t for t in range(len(df)) if t % n == 0]
labels = [l for i, l in enumerate(df["Time(DDHHMM)"]) if i % n == 0]
plt.xticks(ticks, labels)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文