【发布时间】:2012-03-26 04:50:39
【问题描述】:
我正在编写一个快速而肮脏的脚本来动态生成绘图。我使用下面的代码(来自Matplotlib 文档)作为起点:
from pylab import figure, axes, pie, title, show
# Make a square figure and axes
figure(1, figsize=(6, 6))
ax = axes([0.1, 0.1, 0.8, 0.8])
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15, 30, 45, 10]
explode = (0, 0.05, 0, 0)
pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)
title('Raining Hogs and Dogs', bbox={'facecolor': '0.8', 'pad': 5})
show() # Actually, don't show, just save to foo.png
我不想在 GUI 上显示绘图,相反,我想将绘图保存到文件(例如 foo.png)中,以便例如在批处理脚本中使用。我该怎么做?
【问题讨论】:
-
页面下方的许多答案都提到了
plt.close(fig),这在大循环中尤其重要。否则数字保持打开并在内存中等待,所有打开的数字将在执行plt.show()时显示
标签: python matplotlib plot