gpt4 book ai didi

python - Matplotlib 显示重叠的 x-tick 标签

转载 作者:IT老高 更新时间:2023-10-28 21:58:28 28 4
gpt4 key购买 nike

请看下图: enter image description here

这是这个大图的子图: enter image description here

我发现它有两个问题。首先,x 轴标签相互重叠(这是我的主要问题)。第二。 x 轴次要网格线的位置似乎有点不稳定。在图表的左侧,它们看起来间隔适当。但在右侧,它们似乎挤满了主要网格线......好像主要网格线位置不是次要刻度线位置的正确倍数。

我的设置是我有一个名为 df 的 DataFrame,它的行上有一个 DatetimeIndex 和一个名为 value 的列,其中包含 float 。如有必要,我可以在要点中提供 df 内容的示例。十几行 df 在这篇文章的底部供引用。

这是生成图形的代码:

now = dt.datetime.now()

fig, axes = plt.subplots(2, 2, figsize=(15, 8), dpi=200)
for i, d in enumerate([360, 30, 7, 1]):
ax = axes.flatten()[i]
earlycut = now - relativedelta(days=d)
data = df.loc[df.index>=earlycut, :]
ax.plot(data.index, data['value'])
ax.xaxis_date()

ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())
ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())

ax.grid(b=True, which='major', color='w', linewidth=1.5)
ax.grid(b=True, which='minor', color='w', linewidth=0.75)

让 x 轴标签停止相互重叠(在四个子图中)的最佳选择是什么?另外,单独(但不那么紧急),左上角子图中的小勾号问题是怎么回事?

我使用的是 Pandas 0.13.1、numpy 1.8.0 和 matplotlib 1.4.x。

这里有一小段 df 供引用:

                                    id scale  tempseries_id    value
timestamp
2014-11-02 14:45:10.302204+00:00 7564 F 1 68.0000
2014-11-02 14:25:13.532391+00:00 7563 F 1 68.5616
2014-11-02 14:15:12.102229+00:00 7562 F 1 68.9000
2014-11-02 14:05:13.252371+00:00 7561 F 1 69.0116
2014-11-02 13:55:11.792191+00:00 7560 F 1 68.7866
2014-11-02 13:45:10.782227+00:00 7559 F 1 68.6750
2014-11-02 13:35:10.972248+00:00 7558 F 1 68.4500
2014-11-02 13:25:10.362213+00:00 7557 F 1 68.1116
2014-11-02 13:15:10.822247+00:00 7556 F 1 68.2250
2014-11-02 13:05:10.102200+00:00 7555 F 1 68.5616
2014-11-02 12:55:10.292217+00:00 7554 F 1 69.0116
2014-11-02 12:45:10.382226+00:00 7553 F 1 69.3500
2014-11-02 12:35:10.642245+00:00 7552 F 1 69.2366
2014-11-02 12:25:12.642255+00:00 7551 F 1 69.1250
2014-11-02 12:15:11.122382+00:00 7550 F 1 68.7866
2014-11-02 12:05:11.332224+00:00 7549 F 1 68.5616
2014-11-02 11:55:11.662311+00:00 7548 F 1 68.2250
2014-11-02 11:45:11.122193+00:00 7547 F 1 68.4500
2014-11-02 11:35:11.162271+00:00 7546 F 1 68.7866
2014-11-02 11:25:12.102211+00:00 7545 F 1 69.2366
2014-11-02 11:15:10.422226+00:00 7544 F 1 69.4616
2014-11-02 11:05:11.412216+00:00 7543 F 1 69.3500
2014-11-02 10:55:10.772212+00:00 7542 F 1 69.1250
2014-11-02 10:45:11.332220+00:00 7541 F 1 68.7866
2014-11-02 10:35:11.332232+00:00 7540 F 1 68.5616
2014-11-02 10:25:11.202411+00:00 7539 F 1 68.2250
2014-11-02 10:15:11.932326+00:00 7538 F 1 68.5616
2014-11-02 10:05:10.922229+00:00 7537 F 1 68.9000
2014-11-02 09:55:11.602357+00:00 7536 F 1 69.3500

编辑:尝试 fig.autofmt_xdate():我不认为这会奏效。这似乎对左侧的两个图和右侧的两个图都使用了相同的 x-tick 标签。鉴于我的数据,这是不正确的。请看下面有问题的输出:

enter image description here

最佳答案

好的,终于搞定了。诀窍是使用 plt.setp 手动旋转刻度标签。使用 fig.autofmt_xdate() 不起作用,因为当您的图中有多个子图时,它会做一些意想不到的事情。这是带有输出的工作代码:

for i, d in enumerate([360, 30, 7, 1]):
ax = axes.flatten()[i]
earlycut = now - relativedelta(days=d)
data = df.loc[df.index>=earlycut, :]
ax.plot(data.index, data['value'])

ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())
ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())

ax.grid(b=True, which='major', color='w', linewidth=1.5)
ax.grid(b=True, which='minor', color='w', linewidth=0.75)

plt.setp(ax.get_xticklabels(), rotation=30, horizontalalignment='right')

fig.tight_layout()

enter image description here

顺便说一句,前面关于 matplotlib 的一些东西永远占用的评论在这里非常有趣。我正在使用树莓派作为远程位置的气象站。它正在收集数据并通过网络提供结果。天啊,天啊,想把这些图形放出来真的让人喘不过气来。

关于python - Matplotlib 显示重叠的 x-tick 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26700598/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com