我試圖將我的步驟繪制為散點(diǎn)圖,然后最終添加一條趨勢線。我設(shè)法讓它與 df.plot() 一起使用,但它是一個(gè)折線圖。以下是我嘗試過的代碼:import pandas as pdimport matplotlib.pyplot as pltimport numpy as npdata_file = pd.read_csv('CSV/stepsgyro.csv')# print(data_file.head())# put in the correct data typesdata_file = data_file.astype({"steps": int})pd.to_datetime(data_file['date'])# makes the date definitely the index at the bottomdata_file.set_index(['date'], inplace=True)# sorts the data frame by the indexdata_file.sort_values(by=['date'], inplace=True, ascending=True)# data_file.columns.values[1] = 'date'# plot the raw steps data# data_file.plot()plt.scatter(data_file.date, data_file.steps)plt.title('Daily Steps')plt.grid(alpha=0.3)plt.show()plt.close('all')# plot the cumulative steps datadata_file = data_file.cumsum()data_file.plot()plt.title('Cumulative Daily Steps')plt.grid(alpha=0.3)plt.show()plt.close('all')這是它在我的 IDE 上的樣子的屏幕截圖:
3 回答

Smart貓小萌
TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊
您已將索引設(shè)置為“日期”列。從那一刻起,不再有“日期”列,因此data_file.date
失敗。
兩種選擇:
不要設(shè)置索引。無論如何似乎不需要排序。
繪制索引,
plt.scatter(data_file.index, data_file.steps)

喵喵時(shí)光機(jī)
TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
僅通過查看您的示例,我無法弄清楚為什么會(huì)出現(xiàn)該錯(cuò)誤。但是,我可以提供一個(gè)快速簡便的解決方案來繪制您的數(shù)據(jù):
data_file.plot(marker='.', linestyle='none')
添加回答
舉報(bào)
0/150
提交
取消