【问题标题】:MatplotLib not properly plotting pandas timeseries 1 minute data after converting index to datetime object将索引转换为日期时间对象后,MatplotLib 无法正确绘制熊猫时间序列 1 分钟数据
【发布时间】:2021-09-25 02:32:35
【问题描述】:

在将索引转换为日期时间对象后,我无法绘制时间序列 1 分钟的数据。

这是将索引转换为日期时间对象之前的图表。

这是使用将索引更改为日期时间对象后的绘制图表 df.index = pd.to_datetime(df.index)

我不确定这里发生了什么,但是当我转换为 1 分钟的日期时间对象时,看起来 Matplotlib 缺少某些值。

这是https://easyupload.io/koh8s6的数据样本

如果有人能提供帮助将不胜感激。

【问题讨论】:

  • 这可能是因为您的数据在时间上不连续。比如数据可能在昨天下午 5 点结束,第二天早上 8 点开始,是这样吗?
  • 一点也不,数据完全没问题。我刚刚检查了它
  • 你检查过你的数据是按照x轴数据排序的吗?
  • 是的,数据非常好,而且数据似乎排序良好。当索引只是类型字符串时,图表完美绘制。这只发生在使用 pd.to_date_time 将索引更改为日期时间对象时。

标签: python pandas datetime matplotlib time-series


【解决方案1】:

这对我来说似乎工作得很好。您确定使用日期时间类型作为时间索引吗?

df = pd.read_csv('vixdatasample_to_SO.csv', index_col=[1])
df.index = pd.to_datetime(df.index)
df.iloc[:, 2:6].plot()
                     Unnamed: 0  Unnamed: 0.1   Open  Close   High    Low  Volume  ma  std  upper_band  lower_band
Time                                                                                                              
2021-01-19 08:15:00           0             0  23.03  23.01  23.03  23.01     0.0 NaN  NaN         NaN         NaN
2021-01-19 08:16:00           1             1  23.03  23.03  23.05  23.01     0.0 NaN  NaN         NaN         NaN
2021-01-19 08:17:00           2             2  23.04  23.04  23.06  23.04     0.0 NaN  NaN         NaN         NaN
2021-01-19 08:18:00           3             3  23.03  23.03  23.03  23.03     0.0 NaN  NaN         NaN         NaN
2021-01-19 08:19:00           4             4  23.05  23.06  23.06  23.05     0.0 NaN  NaN         NaN         NaN

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 2019-01-15
    • 2016-06-03
    • 2020-01-27
    • 2021-12-29
    • 2018-12-08
    相关资源
    最近更新 更多