译(三十七)-Matplotlib保存为图片

stackoverflow热门问题目录

如有翻译问题欢迎评论指出,谢谢。

代码段的空白行不显示,所以加个注释符。

Matplotlib 保存为图片

  • Homunculus Reticulli asked:

    • 我正在写一个简单的脚本来生成 plot,最开始的代码如下(来自 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
    • 我不想显示 plot,简单的把 plot 保存成文件就行了(像 foo.png 这种),方便在如批处理脚本等地方使用。怎么实现这个需求?

  • Answers:

    • Hooked – vote: 1812

    • 虽然已经有其它回答了,不过我这里还有一些关于 matplotlib.pyplot.savefig 有用的提示。文件格式可以由后缀指定:

    • from matplotlib import pyplot as plt
      #
      plt.savefig('foo.png')
      plt.savefig('foo.pdf')
    • 这将分别使输出栅格化与矢量化,都很有用。此外,对于多余的图片空白部分,可以这样移除:

    • plt.savefig('foo.png', bbox_inches='tight')
    • 注意在显示 plot 时,plt.show() 要在 plt.savefig() 后面,不然图片会变成空白。

    • Demis – vote: 254

    • 正如其它回答所述,plt.savefig() 或者 fig1.savefig() 是有效保存图片的方式。

    • 不过我发现在一些特定情况下 figure 总是会显示(例如 Spyder 的 plt.ion(): interactive mode = On)。为此我在循环中使用 plt.close(figure_object) 来强制关闭 figure 绘制窗(见文档),防止一次循环出现一堆的 figure 窗。

    • import matplotlib.pyplot as plt
      fig, ax = plt.subplots( nrows=1, ncols=1 )  # create figure & 1 axis
      ax.plot([0,1,2], [10,20,3])
      fig.savefig('path/to/save/image/to.png')   # save the figure to file
      plt.close(fig)    # close the figure window
    • 如果需要用 fig.show() 的话,应该需要重开 figure(不过我没试过)。

    • Lukasz Czerwinski – vote: 172

    • 解决:

    • pylab.savefig('foo.png')

Save plot to image file instead of displaying it using Matplotlib

  • Homunculus Reticulli asked:

    • I am writing a quick-and-dirty script to generate plots on the fly. I am using the code below (from Matplotlib documentation) as a starting point:
      我正在写一个简单的脚本来生成 plot,最开始的代码如下(来自 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
    • I don\’t want to display the plot on a GUI, instead, I want to save the plot to a file (say foo.png), so that, for example, it can be used in batch scripts. How do I do that?
      我不想显示 plot,简单的把 plot 保存成文件就行了(像 foo.png 这种),方便在如批处理脚本等地方使用。怎么实现这个需求?

  • Answers:

    • Hooked – vote: 1812

    • While the question has been answered, I\’d like to add some useful tips when using matplotlib.pyplot.savefig. The file format can be specified by the extension:
      虽然已经有其它回答了,不过我这里还有一些关于 matplotlib.pyplot.savefig 有用的提示。文件格式可以由后缀指定:

    • from matplotlib import pyplot as plt
      #
      plt.savefig('foo.png')
      plt.savefig('foo.pdf')
    • Will give a rasterized or vectorized output respectively, both which could be useful. In addition, there\’s often an undesirable, whitespace around the image, which can be removed with:
      这将分别使输出栅格化与矢量化,都很有用。此外,对于多余的图片空白部分,可以这样移除:

    • plt.savefig('foo.png', bbox_inches='tight')
    • Note that if showing the plot, plt.show() should follow plt.savefig(), otherwise the file image will be blank.
      注意在显示 plot 时,plt.show() 要在 plt.savefig() 后面,不然图片会变成空白。

    • Demis – vote: 254

    • As others have said, plt.savefig() or fig1.savefig() is indeed the way to save an image.
      正如其它回答所述,plt.savefig() 或者 fig1.savefig() 是有效保存图片的方式。

    • However I\’ve found that in certain cases the figure is always shown. (eg. with Spyder having plt.ion(): interactive mode = On.) I work around this by forcing the closing of the figure window in my giant loop with plt.close(figure_object) (see documentation), so I don\’t have a million open figures during the loop:
      不过我发现在一些特定情况下 figure 总是会显示(例如 Spyder 的 plt.ion(): interactive mode = On)。为此我在循环中使用 plt.close(figure_object) 来强制关闭 figure 绘制窗(见文档),防止一次循环出现一堆的 figure 窗。

    • import matplotlib.pyplot as plt
      fig, ax = plt.subplots( nrows=1, ncols=1 )  # create figure & 1 axis
      ax.plot([0,1,2], [10,20,3])
      fig.savefig('path/to/save/image/to.png')   # save the figure to file
      plt.close(fig)    # close the figure window
    • You should be able to re-open the figure later if needed to with fig.show() (didn\’t test myself).
      如果需要用 fig.show() 的话,应该需要重开 figure(不过我没试过)。

    • Lukasz Czerwinski – vote: 172

    • The solution is:
      解决:

    • pylab.savefig('foo.png')

You may also like...

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注