Save plot to image file instead of displaying it using Matplotlib

前端 未结 20 1742
一生所求
一生所求 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:21

    I used the following:

    import matplotlib.pyplot as plt
    
    p1 = plt.plot(dates, temp, 'r-', label="Temperature (celsius)")  
    p2 = plt.plot(dates, psal, 'b-', label="Salinity (psu)")  
    plt.legend(loc='upper center', numpoints=1, bbox_to_anchor=(0.5, -0.05),        ncol=2, fancybox=True, shadow=True)
    
    plt.savefig('data.png')  
    plt.show()  
    f.close()
    plt.close()
    

    I found very important to use plt.show after saving the figure, otherwise it won't work.figure exported in png

提交回复
热议问题