在同一个p上绘制两个不同的数据帧

2024-04-25 21:57:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在同一个图上绘制两个不同的数据帧。但它只显示了第二个。我有两个形状相同的数据帧:reconstructedexpected。我需要根据索引(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()

Tags: 数据trueplot绘制locexpectedlayoutlegend
1条回答
网友
1楼 · 发布于 2024-04-25 21:57:37

只需存储第一个绘图的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')

相关问题 更多 >