Automatic detection of display availability with matplotlib

后端 未结 7 1616
悲&欢浪女
悲&欢浪女 2020-12-15 06:26

I\'m generating matplotlib figures in a script which I run alternatively with or without a graphical display. I\'d like the script to adjust automatically: with display, it

7条回答
  •  醉梦人生
    2020-12-15 06:53

    when use GUI backend the figure object has show() method, you can use it to do the switch:

    import matplotlib
    #matplotlib.use("Agg")
    
    import matplotlib.pyplot as plt
    fig = plt.figure()
    havedisplay = False
    if hasattr(fig, "show"):
        plt.show()
    else:
        print "save fig"
        fig.savefig("myfig.png")
    

提交回复
热议问题