我正在尝试绘制股票数据及其显示奇怪的折线图而不是连续线。我无法理解我做错了什么。请任何人都可以帮助我解决这个问题。
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
df = pd.read_csv(r'C:\Users\dipes\Desktop\Data.csv')
df = df.set_index('Date')
df.index = pd.to_datetime(df.index)
#print(df)
plt.figure(figsize=(16,8))
plt.title('Close Price History')
df['Close'].sort_index().plot()
plt.xlabel('Date',fontsize=18)
plt.ylabel('Close price',fontsize=18)
plt.autoscale(tight=True)
plt.show()
答案 0 :(得分:-1)
出于某种原因,默认情况下 pd.to_datetime
以美国格式(即 month-day-year
)解释日期字符串,但它们采用 day-month-year
——合理的格式;)。你必须告诉 Pandas 表现得很好:
df.index = pd.to_datetime(df.index, dayfirst=True)