【问题标题】:Python: Issue with plotting time series (date and values) - weird looking plots w image for referencePython:绘制时间序列(日期和值)的问题 - 看起来很奇怪的绘图 w 图像供参考
【发布时间】:2021-10-21 09:49:51
【问题描述】:

我在绘制时间序列数据时遇到了这个问题,其中索引日期会产生奇怪的图。我也尝试过plot(x='Date', y='values'),但产生了奇怪的情节。

# splitting the column to account for date time.    
new = df["Gmt time"].str.split(" ", n = 1, expand = True)
df["Date"]= new[0]
df["Time"]= new[1]

df['Date'] = pd.to_datetime(df['Date'])

head of data values

df.plot(x='Date', y='Close')

Output of code above

df = df.set_index(df['Date'])

Output of code after setting the date as index.

非常感谢您能告诉我我的代码哪里出了问题或解决以下问题。

【问题讨论】:

  • df.sort_index(inplace=True) 然后绘图。
  • 试过df.sort_index(inplace=True) 问题似乎仍然存在。
  • new 看起来不是日期时间格式。很难确定没有数据。不管怎样,df["Gmt time"] = pd.to_datetime(df["Gmt time"],然后是df['Date'] = df["Gmt time"].dt.datedf['Time'] = df["Gmt time"].dt.time

标签: python matplotlib plot time-series


【解决方案1】:

通过在 read_csv() 中添加 dayfirst=True 解决了问题。 数据取自 dukascopy 的 csv 文件。

【讨论】:

    猜你喜欢
    • 2022-01-13
    • 2018-11-14
    • 2020-07-15
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 2011-03-03
    相关资源
    最近更新 更多