如何旋转轴标签并隐藏其中一些?

2024-04-24 12:17:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着画一个时间序列和它的区别

但是,x轴标签有两个问题:

  1. 它不旋转
  2. 画布上的月份太多,空间太少

如何旋转所有标签并隐藏一些日期

由于保密的原因,我无法显示数据。但它基本上是一个(数字)列,包含序列和(日期)索引

这就是我到目前为止所做的:

import numpy as np, pandas as pd
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
import matplotlib.pyplot as plt
plt.rcParams.update({'figure.figsize':(9,7), 'figure.dpi':120})

# Original Series
fig, axes = plt.subplots(3, 2, sharex=True);
axes[0, 0].plot(df.teste); 
axes[0, 0].set_title('Original Series');
axes[0,0].set_xticklabels(df.index,rotation=90)
plot_acf(df.teste, ax=axes[0, 1]);

# 1st Differencing
axes[1, 0].plot(df.teste.diff()); 
axes[1, 0].set_title('1st Order Differencing');
plot_acf(df.teste.diff().dropna(), ax=axes[1, 1]);

# 2nd Differencing
axes[2, 0].plot(df.teste.diff().diff()); 
axes[2, 0].set_title('2nd Order Differencing');
axes[2,0].set_xticklabels(df.index,rotation=90)
plot_acf(df.teste.diff().diff().dropna(), ax=axes[2, 1]);

这是输出:

enter image description here


Tags: importdfplottitleasdiffplt序列
1条回答
网友
1楼 · 发布于 2024-04-24 12:17:42

检查此代码:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 1000)
y = np.sin(x)

fig, ax = plt.subplots(1, 2, figsize = (8, 4))

ax[0].plot(x, y, 'r-', lw = 2)
ax[0].set_xticks(np.arange(0, 10, 0.25))

ax[1].plot(x, y, 'r-', lw = 2)
ax[1].set_xticks(np.arange(0, 10, 1))
locs, labels = plt.xticks()
plt.setp(labels, rotation = 90)

plt.show()

这给了我一个例子:

enter image description here

如您所见,两个图形都有相同的选项,但在第二个(右侧)中,我设置了:

ax[1].set_xticks(np.arange(0, 10, 1))

xticks隔开以删除其中一些,以及

locs, labels = plt.xticks()
plt.setp(labels, rotation = 90)

旋转他们的方向

相关问题 更多 >