Trouble displaying graphs in Pycharm: Matplotlib using non-GUI backend

I’m having issues showing a simple plot in Pycharm. Here’s what I’m trying:

from matplotlib import pyplot as plt
x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
plt.plot(x, y)
plt.show()

But instead of seeing the graph, I get this warning:

UserWarning: Matplotlib is using agg, a non-GUI backend, so it can't display the figure.

I tried to fix it by changing the backend:

import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib import pyplot as plt

Now I’m getting a different error:

ModuleNotFoundError: No module named 'PyQt5'

I tried to install it with pip, but it didn’t work. How can I get my graphs to show up in Pycharm?

hey max, had similar issue. try installing PyQt5 with pip install PyQt5. if that fails, u could use TkAgg backend instead. just add matplotlib.use(‘TkAgg’) before importing pyplot. works for me in pycharm. good luck!

The issue you’re experiencing is quite common when working with Matplotlib in PyCharm. One effective solution is to use the ‘inline’ backend, which is particularly useful in IDE environments. Add this line before your plotting code:

%matplotlib inline

This magic command tells Matplotlib to render plots directly in the PyCharm output console. If that doesn’t work, you might need to adjust your PyCharm settings. Go to File > Settings > Tools > Python Scientific, and ensure that ‘Show plots in tool window’ is checked. These steps should resolve the graph display problem without requiring additional installations.