【发布时间】:2021-06-15 08:46:25
【问题描述】:
我在 pandas DataFrame 中有时间序列数据,如下所示:
ID HeartRate
1 120
1 118
1 115
2 98
2 110
2 112
3 128
3 115
3 90
我想为每个不同的 ID(即患者)创建一个单独的线图。我怎样才能最好地使用matplotlib呢?我需要创建一个“时间间隔”变量吗?
df = my_data[['ID', 'HR']].copy() ## creating a new "mini" dataframe from the larger one that I've got.
n_ids = df.ID.unique().size
n_cols = int(n_ids ** 0.5)
n_rows = int(n_ids + n_ids % n_cols)
fig, axes = plt.subplots(n_rows, n_cols)
for i, (ids, hr) in enumerate(df.groupby('ID')['HR']):
hr.plot(ax=axes[i], title=f"ID:{idx}")
fig.tight_layout()
但是,当我收到以下错误时:
'numpy.ndarray' object has no attribute 'get_figure'
【问题讨论】:
-
您愿意使用
pandas库吗? -
是的,很抱歉忘记提及我的数据集已作为 pandas 数据框导入
-
分开是指不同
ax上的每个ID? -
我不认为它确实如此,因为据我所知,他们正在那里的一个地块上绘制不同的不同单位,而我想做的是相反的(除非我错过了线程中有东西!)
标签: python pandas matplotlib plot time-series