Can't use matplotlib.use('Agg'), graphs always show on the screen

前端 未结 3 1113
栀梦
栀梦 2021-01-03 06:04

I\'m studying matplotlib and don\'t know how to just save the graph and not print it on the screen.

So I\'ve done some research on the Internet, many answers said

相关标签:
3条回答
  • 2021-01-03 06:54

    plt.plot(x,y) plt.savefig('path/figure_filename.jpg',dpi=300)

    0 讨论(0)
  • 2021-01-03 06:56

    The answer to your original question is simple. If you don't want to show the graph on screen, just don't use plt.show() So what you've gotta do is simply:

    import matplotlib.pylab as plt    
    plt.plot(x,y) #whatever the x, y data be
    #plt.show()  """Important: either comment this line or delete it"""
    plt.savefig('path/where/you/want/to/save/filename.ext') 
    #'filename' is either a new file or an already existing one which will get overwritten at the time of execution. 'ext' can be any valid image format including jpg, png, pdf, etc.
    
    0 讨论(0)
  • 2021-01-03 06:58

    You can try to switch the backend. Apparently Spyder loads matplotlib before you do, and use has no effect. This may be helpful: How to switch backends in matplotlib / Python

    0 讨论(0)
提交回复
热议问题