我正在尝试绘制一个带有TimeStamp indizes的pandas DataFrame,它在其indizes中有一个时间间隔.使用pandas.plot()会在前一个分段的最后一个时间戳与下一个分段的第一个时间戳之间进行线性插值.我既不想线性插值,也不要两个日期段之间的空白.有办法吗?
I am trying to plot a pandas DataFrame with TimeStamp indizes that has a time gap in its indizes. Using pandas.plot() results in linear interpolation between the last TimeStamp of the former segment and the first TimeStamp of the next. I do not want linear interpolation, nor do I want empty space between the two date segments. Is there a way to do that?
假设我们有一个带有TimeStamp的DataFrame:
Suppose we have a DataFrame with TimeStamp indizes:
>>> import numpy as np >>> import pandas as pd >>> import matplotlib.pyplot as plt >>> df = pd.DataFrame(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000)) >>> df = df.cumsum()现在让我们取其中的两个时间块并将其绘制:
Now lets take two time chunks of it and plot it:
>>> df = pd.concat([df['Jan 2000':'Aug 2000'], df['Jan 2001':'Aug 2001']]) >>> df.plot() >>> plt.show()生成的图具有一条插值线,该插值线连接围绕间隙的时间戳记.我无法弄清楚如何在这台机器上上传图片,但是这些图片来自 Google网上论坛显示了我的问题(interpolated.jpg,no-interpolation.jpg和no gaps.jpg).我可以重新创建第一个,如上所示.第二个可以通过用NaN替换所有间隙值来实现(另请参见此问题).如何获得省略时间间隔的第三个版本?
The resulting plot has an interpolation line connecting the TimeStamps enclosing the gap. I cannot figure out how to upload pictures on this machine, but these pictures from Google Groups show my problem (interpolated.jpg, no-interpolation.jpg and no gaps.jpg). I can recreate the first as shown above. The second is achievable by replacing all gap values with NaN (see also this question). How can I achieve the third version, where the time gap is omitted?
推荐答案尝试:
df.plot(x=df.index.astype(str))
您可能需要自定义刻度线和刻度线标签.
You may want to customize ticks and tick labels.
编辑
使用pandas 0.17.1和numpy 1.10.4对我有效.
That works for me using pandas 0.17.1 and numpy 1.10.4.
您真正需要的是将DatetimeIndex转换为与日期时间不一样的另一种类型的方法.为了获得有意义的标签,我选择了str.如果x=df.index.astype(str)不适用于您的pandas/numpy/任何组合,您可以尝试其他选择:
All you really need is a way to convert the DatetimeIndex to another type which is not datetime-like. In order to get meaningful labels I chose str. If x=df.index.astype(str) does not work with your combination of pandas/numpy/whatever you can try other options:
df.index.to_series().dt.strftime('%Y-%m-%d') df.index.to_series().apply(lambda x: x.strftime('%Y-%m-%d')) ...我意识到没有必要重置索引,因此我删除了该部分.
I realized that resetting the index is not necessary so I removed that part.
更多推荐
Python大 pandas 绘制带有时间间隔的时间序列
发布评论