简体   繁体   English

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

[英]plotting two different dataframes on the same plot

I'm trying to plot two different dataframes on the same plot.我试图在同一个图上绘制两个不同的数据框。 But it only shows the second one.但它只显示第二个。 I have two dataframes: reconstructed and expected with the same shape.我有两个数据帧: reconstructedexpected的形状相同。 I need to plot them based on indexes ( idx ).我需要根据索引( idx )绘制它们。 So first I need to partition them based on each index;所以首先我需要根据每个索引对它们进行分区; that is done by ts_rec = reconstructed.loc[idx] and ts_exp = expected.loc[idx] .这是由ts_rec = reconstructed.loc[idx]ts_exp = expected.loc[idx] Then I should plot these two new dataframes.然后我应该绘制这两个新的数据框。 Each of them has 28 columns, so I have 28 subplots with layout=(7, 4).他们每个人都有 28 列,所以我有 28 个子图,layout=(7, 4)。 The problem is that it only shows the second (red) timeseries, but I need to have both of them to be able to compare their values.问题是它只显示第二个(红色)时间序列,但我需要让它们都能够比较它们的值。 How can I fix this?我怎样才能解决这个问题?

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()

You just need to store the ax handle from the first plot and pass it as ax argument to the second plot:您只需要存储第一个图中的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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM