24

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.

Community
  • 1
  • 1
Savage Henry
  • 1,617
  • 3
  • 18
  • 27
  • 3
    I've duplicated this behavior with Pycharm 4.0 Pro on OSX 10.10. It appears to be an issue within Pycharm's Python console, as indicated by https://youtrack.jetbrains.com/issue/PY-14547. – Jason Feb 21 '15 at 13:23
  • So perhaps the answer is dropping PyCharm. Bleargh. I've edited my question to reflect this issue, and to ask if there is a known workaround. – Savage Henry Feb 21 '15 at 13:36
  • Why drop pycharm, since Console is basically a duplicate of the functionality within Terminal? You can do everything inside Terminal that you can with Console... just make sure you're in the right folder first. – Jason Feb 21 '15 at 13:40
  • 1
    The big value to me of using pycharm with it's own ipython console (rather than the terminal it provides) is the interactive process of sending bits of code for evaluation. Using just the terminal requires either working entirely in the terminal to type in, then evaluate snippets of code I want to try, or copy-and-pasting them from the editor window. – Savage Henry Feb 21 '15 at 13:43
  • Makes sense. Do you have a Linux box that you could use to test if this issue occurs with the linux variant? I have to wonder if this is an OS-specific bug. – Jason Feb 21 '15 at 13:46
  • I do, and will check that. In the meantime, it looks like one answer might be to skip matplotlib and learn ggplot. – Savage Henry Feb 21 '15 at 14:07
  • 1
    Python bindings for gnuplot might be an alternative... http://gnuplot-py.sourceforge.net/ – Jason Feb 21 '15 at 14:50
  • @SavageHenry ggplot is just a wrapper on top of matplotlib – tacaswell Feb 23 '15 at 03:04
  • also try using one of the other backends (Qt, wx, tk) – tacaswell Feb 23 '15 at 03:05
  • @tcaswell thanks, I didn't realize that. As I noted in my post, I've tried all the available backends, to no avail. Interestingly, using ggplot (before seeing your comment) gets a plot to SHOW, but the process still hangs, the Figure 1 box is unresponsive, and there's no way to get back to working without quitting python. – Savage Henry Feb 23 '15 at 19:27
  • 6
    https://youtrack.jetbrains.com/issue/PY-14547 Looks like a fix has been implemented and will be included in the next update. – Jason Mar 04 '15 at 15:04
  • Excellent, thank you! – Savage Henry Mar 04 '15 at 20:19
  • Similar to this here, when I create a graph, an empty window opens in addition. I do not exactly know since when this is the case, but it wasn't this way earlier. – mstuebner Jul 19 '15 at 20:28
  • @Jason I would suggest adding an answer, as this question seems open still. It looks like a suitable solution is available (i.e. update). – Juan Carlos Coto Nov 16 '15 at 20:53

1 Answers1

1

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.

sorin
  • 137,198
  • 150
  • 472
  • 707
  • I'm using PyCharm Community Edition 2016.2 w/ IPython 5.0 and a python 3.5.2 interpreter on Mac OsX 10.11. I've checked all the paths, and they all match, but I have this same issue still. I can open a figure w/ plt.figure(), but I just get SPOD. Calling plt.plot() shows the lines objects, but never plots to a window (whether it was there before or not). Console seems _really_ slow thereafter. Using MacOSX backend. – jspencer Sep 03 '16 at 01:04