“UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.” when plotting figure with pyplot on Pycharm

前端 未结 14 616
梦谈多话
梦谈多话 2020-12-22 18:33

I am trying to plot a simple graph using pyplot, e.g.:

import matplotlib.pyplot as plt
plt.plot([1,2,3],[5,7,4])
plt.show()

but the figure

相关标签:
14条回答
  • 2020-12-22 19:12

    In my case, the error message was implying that I was working in a headless console. So plt.show() could not work. What worked was calling plt.savefig:

    import matplotlib.pyplot as plt
    
    plt.plot([1,2,3], [5,7,4])
    plt.savefig("mygraph.png")
    

    I found the answer on a github repository.

    0 讨论(0)
  • 2020-12-22 19:12

    I too had this issue in PyCharm. This issue is because you don't have tkinter module in your machine.

    To install follow the steps given below (select your appropriate os)

    For ubuntu users

     sudo apt-get install python-tk
    

    or

     sudo apt-get install python3-tk
    

    For Centos users

     sudo yum install python-tkinter
    

    or

     sudo yum install python3-tkinter
    

    For Windows, use pip to install tk

    After installing tkinter restart your Pycharm and run your code, it will work

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