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

前端 未结 3 1135
栀梦
栀梦 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: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.
    

提交回复
热议问题