gpt4 book ai didi

python - 在同一个图上绘制两个不同的数据框

转载 作者:行者123 更新时间:2023-12-01 08:26:00 27 4
gpt4 key购买 nike

我正在尝试在同一个绘图上绘制两个不同的数据框。但它只显示第二个。我有两个数据框:具有相同形状的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()

最佳答案

您只需存储第一个绘图中的 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 - 在同一个图上绘制两个不同的数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54249657/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com