pythonmatplotlibpycharm

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


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 does not appear and I get the following message:

UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.

I found and tried some advice to re-configure the "backend" mentioned in that warning, like so:

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

but this gives me an error message:

ModuleNotFoundError: No module named 'tkinter'

I assumed that I had to install this module separately, but pip install tkinter does not work:

Collecting tkinter
  Could not find a version that satisfies the requirement tkinter (from versions: )
No matching distribution found for tkinter

How can I make Matplotlib display the graph?


See also: Why does tkinter (or turtle) seem to be missing or broken? Shouldn't it be part of the standard library? . This question is not a duplicate, because the answers discuss other backends besides the Tkinter one.

Also see _tkinter.TclError: no display name and no $DISPLAY environment variable for issues with attempts to use Matplotlib remotely.


Solution

  • Solution 1: is to install the GUI backend tk

    I found a solution to my problem (thanks to the help of ImportanceOfBeingErnest).

    All I had to do was to install tkinter through the Linux bash terminal using the following command:

    sudo apt-get install python3-tk
    

    instead of installing it with pip or directly in the virtual environment in Pycharm.

    Solution 2: install any of the matplotlib supported GUI backends

    NOTE: