python - 在同一个图上绘制两个不同的数据框
问题描述
我试图在同一个图上绘制两个不同的数据框。但它只显示第二个。我有两个数据框:reconstructed
并且expected
形状相同。我需要根据索引 ( idx
) 绘制它们。所以首先我需要根据每个索引对它们进行分区;这是由ts_rec = reconstructed.loc[idx]
和完成的ts_exp = expected.loc[idx]
。然后我应该绘制这两个新的数据框。他们每个人都有 28 列,所以我有 28 个布局=(7、4)的子图。问题是它只显示第二个(红色)时间序列,但我需要让它们都能够比较它们的值。我怎样才能解决这个问题?
ts_rec = reconstructed.loc[idx]
ts_exp = expected.loc[idx]
x = np.arange(ts_rec.shape[0])
ts_rec.plot(
x=x, subplots=True, layout=(7, 4), lw=2, legend=False,
figsize=(12, 10), sharey=True, color='green')
ts_exp.plot(
x=x, subplots=True, layout=(7, 4), lw=2, legend=False,
figsize=(12, 10), sharey=True, color='red')
pyplot.title("Timeseries id = %d" % idx)
pyplot.xlim(xmin=0)
pyplot.show()
pyplot.savefig(config['dir'] + 'ts_' + str(idx) + '.pdf')
pyplot.clf()
解决方案
您只需要存储ax
第一个图的句柄并将其作为ax
参数传递给第二个图:
plt_ax = ts_rec.plot(
x=x, subplots=True, layout=(7, 4), lw=2, legend=False,
figsize=(12, 10), sharey=True, color='green')
ts_exp.plot(
ax=plt_ax, x=x, subplots=True, layout=(7, 4), lw=2, legend=False,
figsize=(12, 10), sharey=True, color='red')
推荐阅读
- python - merge_asof 多列
- ios - 在两个耳机通道上打开 AKMicrophone 工作
- flutter - 如何使用相同的导航栏但不同的页面/FAB
- vba - 在附件中搜索文本
- javascript - 如何使用带有切换删除代码的javascript变量?
- c++ - 如何读取证书以使用 openssl 验证签名?
- c# - 如何识别任务栏设置“在桌面模式下自动隐藏任务栏”设置为开
- sql-server - 是否可以获得单个记录的 PERCENT_RANK,但相对于整个表?
- java - **[更新]** 如何在 android 中动画化卡片视图的背景扩展?
- java - Java中原子set()和getAndSet()方法的区别