如何同时plot两张图片,不是动态图(为了对比)

因为想要对比两张图的,所以需要同时绘出
但matplotlib在遇到plt.show()函数在阻塞模式时会挂起,
代码并不会继续运行,这就导致只能一张一张的显示,不方便对比。
解决方法
利用plot.ion()函数,调节到交互模式具体流程如下:

plt.ion()#代开交互
plt.figure(2, figsize=(32, 20))#画第一个图
plt.plot(pre_data[:,13],pre_data[:,0],'r')
plt.show()

plt.figure(1, figsize=(32, 20))#画第二个图
# Plot the actual value
# plt.plot(true_data[2,:],true_data[0,:], 'b-', label = 'actual')
plt.plot(pre_data[:,13],pre_data[:,0], 'b-', label = 'actual')
# Plot the predicted values
plt.plot(true_data[2,:],true_data[1,:], 'r.', label = 'prediction')
plt.xticks(rotation = '60')
plt.legend(loc='best', prop = {'size':30})
# Graph labels
plt.xlabel('Date')
plt.ylabel(' target O3 (ug/m^3)') 
plt.title('Actual and Predicted Values')

plt.ioff() 
#一定注意plot.ioff()一定在show()前面,不然就是一闪而过
plt.show()

在这里插入图片描述
解决了你的问题的话,记得点赞,欢迎指正。


版权声明:本文为john_ashley原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。