问题描述
我有一个要绘制的时间序列数据.晚上不收集数据的时候,晚上9点到早上7点之间有一个时间间隔,在图表上看起来有点难看,很难阅读.
I have a timeseries of data I would like to plot. In the night, when i do not collect data, I have a gap between 9 pm and 7 am which looks a bit ugly on the chart and makes it hard to read.
这里有一个小例子来理解这个问题:
here is a little example to understand the issue:
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()
从 2000 年 1 月 1 日到 2000 年 1 月 3 日的图表几乎无法阅读,因为该图被缩放以显示 2005 年的数据.有没有办法消除 1/3 中的指数 (?)/2000 到 1/3/2005?
the graph from 1/1/2000 to 1/3/2000 is almost unreadable, because the plot is scaled to show also the data from 2005. is there a way to eliminate that the indices (?) from 1/3/2000 to 1/3/2005?
谢谢和欢呼,E.
推荐答案
IIUC,让我创建一个样本集和糟糕的结果.
IIUC, let me create a sample set and bad outcome.
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()
输出:
但是,我们可以像这样消除那些扁平的部分:
However, we can eliminate those flatline sections like this:
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)
输出:
这篇关于 pandas 图聚合时间戳索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!