Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 632
Savage Henry Avatar asked Feb 21 '15 13:02

Savage Henry


People also ask

Why is my plot not showing up in python?

It means if we are not using the show() function, it wouldn't show any plot. When we use the show() function in the non-interactive mode. That means when we write the code in the file it will show all the figures or plots and blocks until the plots have been closed.


1 Answers

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.

like image 149
sorin Avatar answered Oct 12 '22 03:10

sorin