Savage Henry
Savage Henry

Reputation: 2069

PyCharm won't open matplotlib plots correctly

I have an issue with PyCharm and matplotlib that I cannot seem to correct.

When I use PyCharm and ipython as the console through which commands are interpreted, plots do not show up until I save the figure. However, this does not happen when I attempt to plot from outside PyCharm.

Here's an exmaple process that fails in PyCharm:

In[2]: import matplotlib.pyplot as plt
Backend MacOSX is interactive backend. Turning interactive mode on.

In[3]: plt.plot([1,2,3,4],[1,4,9,16], 'ro')

This will open a window with the label "Figure 1". However, no plot appears, and the mouse switches to the SPOD when hovering over it (I'm on a Mac). At this point, I can try

In[4]: plt.show()

but still no plot appears. However, if I then do:

In[5]: plt.savefig('foo.png')

not only does a figure get saved with that name, the plot appears in the Figure 1 window. Note that PyCharm has set interactive mode on, and that it has recognized that my backend is (should be?) MacOS.

I have use the same process through both a python and ipython console in a generic terminal session, and even through emacs, and in all cases the plot appears on calling the plt.plot() line. No plt.show() call is required.

Indeed, I can even use the terminal through PyCharm and get the desired plot. One answer is that I should just do my plotting using these methods and ignore PyCharm. Assume, however, that I find the "bells and whistles" of PyCharm appealing enough that I would really like to use it as my IDE, but want to be able to examine plots without having to go through the plt.savefig() hassle (since I don't want to have to keep every plot I attempt).

I have also tried switching backends through plt.switch_backend(). I have tried all the options listed, all of which fail for reasons mostly having to do with not having certain packages installed or being on the wrong system. I have not tried installing anything to use a new backend, so I don't know if another, properly installed backend would solve this problem. More than willing to try, of course!

System details:

MacBookPro Retina mid-2012, 16GB ram, Yosemite 10.10

Python 3.4.2

ipython 2.3.1

PyCharm Community Edition 4.0.4

Note that I have reviewed and attempted variations of fixes from the following questions:

Python plots won't open

How to switch backends in matlab/python

PyCharm + Matplotlib?

Matplotlib figure stucked (grey window)

matplotlib does not show my drawings although I call pyplot.show()

EDIT As noted in the comment below, this appears to be a bug, not a setting failure on my part. As such, I'm amending this question to ask if there is a known workaround.

Upvotes: 27

Views: 24770

Answers (4)

Peter Drake
Peter Drake

Reputation: 452

I was able to fix this problem by adding these two lines before plotting:

import matplotlib
matplotlib.use('TkAgg')

Better yet is to install the default matplotlibrc file described at

https://matplotlib.org/stable/users/explain/customizing.html#customizing

and then change the line

##backend: Agg

to:

backend: TkAgg

Upvotes: 0

Tyler DeGroff
Tyler DeGroff

Reputation: 1

It looks like this question is quite old, however I came across this same issue today and this post is the top hit when Googling. All that has to be done here is turning on interactive mode via plt.ion().

import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt

_ = plt.ion()  # turn on interactive mode

mean = 0
std = 1
x = np.linspace(mean - 3 * std, mean + 3 * std, num=100)
y = stats.norm.pdf(x, mean, std)

fig, ax = plt.subplots()

ax.plot(x, y)
ax.set_title(label='normal distribution')
ax.set_xlabel('x values')
ax.set_ylabel('probability density')
ax.grid(True)

plt.show()

Upvotes: -1

Hosein Hashemi
Hosein Hashemi

Reputation: 1

For those still having this issue.

Try installing QtAgg.

pip install pyqt6

And then import matplotlib and use QtAgg as follows:

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

Hope this solves it.

Upvotes: -1

sorin
sorin

Reputation: 170806

There is a known problem with PyCharm (including latest v5 version) under OS X that is caused by the way Python can be installed on OS X.

Most people do install Python 2 and 3 using brew which also replaces the default Python interpreter to be used from the command line. Still, the default interpreter and pip executables are not also replaced for GUI applications.

If you want a more specific answer you will have to properly document your environment with:

  • do which -a python from command line
  • do which -a pip from command line
  • do print(sys.path) and print(os.env) from inside PyCharm and console, and compare them.

Probably by doing this you will realize that the wrong version of the modules are loaded.

Upvotes: 2

Related Questions