熊猫绘制总时间戳索引

时间:2018-11-01 13:04:17

标签: python pandas plot

我有一个要绘制的时间序列数据。晚上,当我不收集数据时,我在晚上9点到早上7点之间有一个缝隙,在图表上看起来有点难看,难以阅读。

下面是一个了解此问题的小例子:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


df2 = pd.DataFrame({ 'A' : pd.Series(np.random.randn(4),index=list(range(4)),dtype='float32'),
                    'B' : pd.date_range('1/1/2000', periods=4)})




print(df2.to_string())
df2.ix[3,'B'] = pd.to_datetime('2005-01-02')

print(df2.to_string())

df2.index = df2.B
fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(df2.index, df2["A"])
plt.show()

从1/1/2000到1/3/2000的图形几乎不可读,因为该图已按比例缩放以显示2005年的数据。是否有一种方法可以消除1/3的索引(?) / 2000至1/3/2005?

谢谢和欢呼, E。

1 个答案:

答案 0 :(得分:3)

IIUC,让我创建一个样本集并得出不良结果。

np.random.seed(0)
df = pd.DataFrame(np.random.random(500), index=pd.date_range('2018-11-25 07:00:00', periods=500, freq='10T'))
df2 = df[(df.index.hour >= 7) & (df.index.hour < 21)]
df2.plot()

输出:

enter image description here

但是,我们可以消除像这样的扁平部分:

np.random.seed(0)
df = pd.DataFrame(np.random.random(500), index=pd.date_range('2018-11-25 07:00:00', periods=500, freq='10T'))

df2 = df[(df.index.hour >= 7) & (df.index.hour < 21)]

df2.index = df2.index.strftime('%Y-%m-%d')

fig, ax = plt.subplots()
_ = df2.plot(ax=ax)
skip = df2.shape[0]//7 + 1
label = [i for i in df2.index[::skip]]
_ = plt.xticks(np.arange(0,df2.shape[0],skip),label,rotation=45)

输出:

enter image description here

相关问题