将数据帧图合并为单个图形



我正试图将任意数量的折线图合并到一个图像中,尽管这类事情有很多问题,但似乎没有一个适用于我正在使用的代码。

与大量的答案不同,我不想在一个输出中并排或重叠显示单独的图形,而是将它们组合在一起。

对于所有这些图;y_ x";列将是相同的;yhat_y";在每个循环期间产生的将是不同的。

在数据帧的绘图方法中添加subplots = True似乎会将返回类型更改为与代码numpy.ndarray' object has no attribute 'get_figure'不再兼容的类型

#ax = plt.subplot(111) doesnt seem to do anything
for variable in range(max_num):
forecast = get_forecast(variable)
cmp1 = forecast.set_index("ds")[["yhat", "yhat_lower", "yhat_upper"]].join(
both.set_index("ds")
)
e.augmented_error[variable]= sklearn.metrics.mean_absolute_error(
cmp["y"].values, cmp1["yhat"].values
)
cmp2=cmp.merge(cmp1,on='ds')
plot = cmp2[['y_x', 'yhat_y']].plot(title =e)
fig1 = plot.get_figure()
plot.set_title("prediction")

plt.show()
fig1.savefig('output.pdf', format="pdf")
plt.close()

最简单的方法是在循环外创建一个可重用的ax句柄,然后在循环内调用ax.plot

fig, ax = plt.subplots() # create reusable `fig` and `ax` handles
for variable in range(max_num):
...
ax.plot(cmp2['y_x'], cmp2['yhat_y']) # use `ax.plot(cmp2...)` instead of `cmp2.plot()`
ax.set_title('predictions')
fig.savefig('output.pdf', format='pdf')

最新更新