SBad
SBad

Reputation: 1345

plt.show() not working in pycharm

I am using pycharm after upgrading my python to python 3.5.

I re-run a standard code that i had in place and had a problem with plt.show() example:

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

The suggestion by DavidG made things worked fine. But this time when I do

       import matplotlib
        matplotlib.use('TkAgg')
        import matplotlib.pyplot as plt
        plt.plot([1,2,3,4])
        plt.show()

i get an error saying

/apps/qtrinst/install/python/anaconda/envs/sx_anaconda/lib/python3.5/site-packages/matplotlib/__init__.py:1401: UserWarning:  This call to matplotlib.use() has no effect
because the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

It didnt get this error before-not sure what happened there.

Upvotes: 3

Views: 8217

Answers (2)

typhoon108
typhoon108

Reputation: 91

Try using a different Backend. It worked for me when I used QtAgg

PyQt

you will need to install some version of PyQt. At the moment:

pip install PyQt6

Specify the GUI backend

import matplotlib
matplotlib.use("QtAgg")

Try plt.show()

from matplotlib import pyplot as plt

# some code here

plt.show()

This worked flawlessly for me. Hope It worked for you too.

Upvotes: 1

DavidG
DavidG

Reputation: 25370

I think the problem is with your "backend". The documentation has a section entitled "What is a backend?" which will be helpful.

I'm not familiar with WebAgg but I don't think you want to be using it. A more conventional one might be TkAgg which requires Tkinger or Qt4Agg which requires PyQt4. You can switch backends using

import matplotlib
matplotlib.use("TkAgg")  # Do this before importing pyplot!
import matplotlib.pyplot as plt 

Upvotes: 5

Related Questions