Save plot to image file instead of displaying it using Matplotlib

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

    You can either do:

    plt.show(hold=False)
    plt.savefig('name.pdf')
    

    and remember to let savefig finish before closing the GUI plot. This way you can see the image beforehand.

    Alternatively, you can look at it with plt.show() Then close the GUI and run the script again, but this time replace plt.show() with plt.savefig().

    Alternatively, you can use

    fig, ax = plt.figure(nrows=1, ncols=1)
    plt.plot(...)
    plt.show()
    fig.savefig('out.pdf')
    

提交回复
热议问题