“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 615
梦谈多话
梦谈多话 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 18:51

    Just in case if this helps anybody.

    Python version: 3.7.7 platform: Ubuntu 18.04.4 LTS

    This came with default python version 3.6.9, however I had installed my own 3.7.7 version python on it (installed building it from source)

    tkinter was not working even when the help('module') shows tkinter in the list.

    The following steps worked for me:

    1. sudo apt-get install tk-dev.

    rebuild the python: 1. Navigate to your python folder and run the checks:

    cd Python-3.7.7
    sudo ./configure --enable-optimizations
    
    1. Build using make command: sudo make -j 8 --- here 8 are the number of processors, check yours using nproc command.
    2. Installing using:

      sudo make altinstall
      

    Don't use sudo make install, it will overwrite default 3.6.9 version, which might be messy later.

    1. Check tkinter now
      python3.7 -m tkinter
      

    A windows box will pop up, your tkinter is ready now.

    0 讨论(0)
  • 2020-12-22 18:54

    If you use Arch Linux (distributions like Manjaro or Antegros) simply type:

    sudo pacman -S tk
    

    And all will work perfectly!

    0 讨论(0)
  • 2020-12-22 18:54

    You can change the matplotlib using backend using the from agg to Tkinter TKAgg using command

    matplotlib.use('TKAgg',warn=False, force=True)
    
    0 讨论(0)
  • 2020-12-22 18:56

    I added %matplotlib inline and my plot showed up in Jupyter Notebook.

    0 讨论(0)
  • 2020-12-22 18:56

    When I ran into this error on Spyder, I changed from running my code line by line to highlighting my block of plotting code and running that all at once. Voila, the image appeared.

    0 讨论(0)
  • Linux Mint 19. Helped for me:

    sudo apt install tk-dev
    

    P.S. Recompile python interpreter after package install.

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