简体   繁体   English

Matplotlib在使用savefig保存图形时不会忘记以前的数据

[英]Matplotlib doesn't forget previous data when saving figures with savefig

import matplotlib.pyplot as plt
plt.plot([1,2,3],[1,2,3],'ro')
plt.axis([-4,4,-4,4])
plt.savefig('azul.png')
plt.plot([0,1,2],[0,0,0],'ro')
plt.axis([-4,4,-4,4])
plt.savefig('amarillo.png')

Output: 输出:

阿祖尔 阿马里洛

Why does this happen and how to solve? 为什么会发生这种情况以及如何解决?

What you see is a completely expected behaviour. 您看到的是完全预期的行为。 You can plot as many data as often as you want to the same figure, which is very often very useful. 您可以在同一图形上绘制任意数量的数据,这通常非常有用。

If you want to create several figures in the same script using the matplotlib state machine, you need to first close one figure before generating the next. 如果要使用matplotlib状态机在同一脚本中创建多个图形,则需要先关闭一个图形,然后再生成下一个图形。

So in this very simple case, just add plt.close() between figure creation. 因此,在这种非常简单的情况下,只需在图形创建之间添加plt.close()

import matplotlib.pyplot as plt
plt.plot([1,2,3],[1,2,3],'bo')
plt.axis([-4,4,-4,4])
plt.savefig('azul.png')
plt.close()
plt.plot([0,1,2],[0,0,0],'yo')
plt.axis([-4,4,-4,4])
plt.savefig('amarillo.png')

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

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