[Matplotlib-users] Can't get interactive plots working on OSX (Catalina)
newville at cars.uchicago.edu
Sun Feb 9 11:55:38 EST 2020
On Thu, Feb 6, 2020 at 7:41 PM jandyman <andy at voelkel.us> wrote:
> I've spent several hours trying to figure out how to get interactive plots
> to work on MacOS Catalina (plots which don't block program execution). I
> fairly excited when I found instructions in the Matplotlib docs for using
> python as a framework and then using the macosx backend, but it doesn't
> to help.
I also find the 'macosx' backend to be challenging for interactive work.
It seems you have to keep hitting return in the main python REPL for the
'macosx' event loop to process. The other backends do not need this. In
particular, the 'qt5agg' and 'tkagg' backends will do what (I think) you
are looking for: `pyplot.ion()` will make `pyplot.plot()` draw to the
screen immediately with a responsive plot window and not block the REPL.
The 'wxagg' backend should be able to do this, but it appears that
`pyplot.ion()` does not actually work as it does for 'qt5agg' and 'tkagg'
- I don't know why that it is.
You might find `wxmplot` (https://newville.github.io/wxmplot/ -- I am the
author of this) useful for interactive work from the Python REPL (or from
IPython for that matter). With this, you would do something like:
>>> import numpy as np
>>> from wxmplot.interactive import plot
>>> x = np.linspace(0, 20, 100)
>>> pd = plot(x, np.sin(x))
This will immediately bring up a separate plot window and returning control
to the REPL. The window is highly interactive: you can zoom in, save
images, and alter nearly every aspect of the plot (colors, themes,
linewidths, styles, markers, text labels, margins, legend display, log
scales, etc) from a configuration panel. You can also easily plot to
multiple window frames, say with
>>> pd2 = plot(x, np.cos(x), win=2)
Of course, not every aspect of matplotlib is exposed in `wxmplot`. The
return value from `plot` has a `panel.axes` attribute that is the
`matplotlib.axes`, which can be used for more complicated plot components.
There is also a `wxmplot.interactive.imshow` for showing false-color images
from 2-d arrays that also has many interactive options (color tables,
contrast, smoothing, etc). `wxmplot` can be installed with `pip install
There is a nasty problem with Anaconda Python on MacOS that `python` is not
a framework build and so the wxPython module cannot draw to the screen, and
trying to do so exits python. Instead, one needs to use `pythonw` (from
the `python.app` conda package) to use wxPython with Anaconda Python on
MacOS. I do not know why this is. This is merely irritating for running
scripts or applications, but is particularly problematic when using IDEs
such as Spyder or Idle, as they embed a Python interpreter that has been
launched with `python`, not `pythonw`. For me, VSCode works fine as it
uses python from Python.org which does not have this problem.
Hope that helps,
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Matplotlib-users