我试图将我的步骤绘制为散点图,然后最终添加一条趋势线。我设法让它与 df.plot() 一起使用,但它是一个折线图。
以下是我尝试过的代码:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data_file = pd.read_csv('CSV/stepsgyro.csv')
# print(data_file.head())
# put in the correct data types
data_file = data_file.astype({"steps": int})
pd.to_datetime(data_file['date'])
# makes the date definitely the index at the bottom
data_file.set_index(['date'], inplace=True)
# sorts the data frame by the index
data_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 data
data_file = data_file.cumsum()
data_file.plot()
plt.title('Cumulative Daily Steps')
plt.grid(alpha=0.3)
plt.show()
plt.close('all')
这是它在我的 IDE 上的样子的屏幕截图:
Smart猫小萌
喵喵时光机
紫衣仙女
相关分类