Python保存空图 [英] Python saving empty plot
问题描述
这个问题是重复的.原始问题的链接已发布在上方.
This question is a duplicate. Original question's link is posted above.
我正在使用Python绘制一组值,这些值在Python终端上显示良好(使用Jupyter NoteBook),但是当我保存它时,打开时保存的文件显示空白(完全白色)的照片.这是我的代码:
I am using Python to plot a set of values which are showing good on the Python terminal (using Jupyter NoteBook) but when I save it, the saved file when opened shows an empty (completely white) photo. Here's my code:
import matplotlib.pyplot as plt
plt.plot([1,3,4])
plt.show()
plt.savefig('E:/1.png')
推荐答案
您应该在关闭绘图之前保存该绘图:实际上,plt.show()
放置该绘图并阻塞执行,直到您关闭它为止.因此,当您尝试保存下一条指令时,该图已被破坏,无法保存.
You should save the plot before closing it: indeed, plt.show()
dislays the plot, and blocks the execution until after you close it; hence when you attempt to save on the next instruction, the plot has been destroyed and can no longer be saved.
import matplotlib.pyplot as plt
plt.plot([1,3,4])
plt.savefig('E:/1.png') # <-- save first
plt.show() # <-- then display
这篇关于Python保存空图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!