Matplotlib shows 'agg backend' warning and won't display plots in PyCharm IDE

I’m having trouble displaying a basic chart using matplotlib in PyCharm. When I run this code:

import matplotlib.pyplot as plt
plt.plot([2,4,6],[8,3,9])
plt.show()

The plot window doesn’t open and I see this warning message:

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

I tried to fix this by changing the backend like this:

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

But then I get this error:

ModuleNotFoundError: No module named 'tkinter'

When I try to install tkinter using pip, it fails:

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

What’s the best way to solve this issue and get matplotlib to show my plots properly?

tkinter is already part of Python’s standard library and does not need to be installed separately. If you’re facing a ModuleNotFoundError, it suggests that your Python installation might be incomplete or you may be using a version that lacks tkinter. I’ve encountered similar issues with minimal Python distributions. Consider using the Qt5Agg backend instead of TkAgg; you can set this up by adding matplotlib.use('Qt5Agg') after installing PyQt5 via pip install PyQt5. Additionally, you can enable PyCharm’s scientific mode by navigating to Settings > Tools > Python Scientific and checking the corresponding box, which will automatically handle backend configurations and display plots directly within the IDE.

check your pyCharm settings first - go to run configurations and enable “run with python console.” this usually fixes backend issues without touching matplotlib settings. also try running the same code in terminal to see if it’s pyCharm-specific.

Hmm, interesting - what OS are you on? Sometimes this stuff’s OS-specific. Try plt.savefig('test.png') instead of plt.show() to see if matplotlib works at all. That’ll tell us if it’s just a display problem or something worse.