【发布时间】: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'])
df.plot(x='Date', y='Close')
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.date和df['Time'] = df["Gmt time"].dt.time
标签: python matplotlib plot time-series