Save plot to image file instead of displaying it using Matplotlib

前端 未结 20 1930
一生所求
一生所求 2020-11-22 06:07

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:

from pylab         


        
20条回答
  •  梦谈多话
    2020-11-22 06:30

    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:

    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, you'll find that pylab leaves a generous, often undesirable, whitespace around the image. Remove it with:

    savefig('foo.png', bbox_inches='tight')
    

提交回复
热议问题