[Matplotlib-users] Can't get interactive plots working on OSX (Catalina)

Hi,

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 got
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 seem
to help. I'm operating in debug mode from Visual Studio Code, which
otherwise seems to work well. Here's the code:

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

matplotlib.use("macosx")
plt.ion()
x = np.linspace(0, 20, 100) # Create a list of evenly-spaced numbers over
the range
plt.plot(x, np.sin(x)) # Plot the sine of each x point
plt.show()
# put a breakpoint on this next line
y = 5

If I omit the call to plt.ion(), I get a plot that blocks program execution
until I close the plot window, so I don't hit the breakpoint until the plot
is gone. If I leave the call to plot.ion() in, I hit the breakpoint, but no
plot is shown.

Does anyone know if is possible to get this to work, and if so, how? I've
got this sort of thing to work before with Spyder, but it is hinky, and I've
got other issues with Spyder. I'm just chasing the sort of user experience I
used to have with Matlab, only with Python, which I like better as a
language.

A lot of folks seems to use Jupyter for this sort of thing, and Jupyter is
great for what it does, but it isn't what I'm trying to do.

···

--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

Jandyman,

The short answer: something has to run the GUI event loop while the prompt is waiting for user input. This is typically called an “input hook”.

For a long answer see https://github.com/matplotlib/matplotlib/pull/4779 .

Unfortunately I am not familiar enough with vscode’s debug mode to provide any guidance on how to configure it in that context.

Tom

···

Thomas Caswell
tcaswell@gmail.com

Yes, I'm aware of the event loop issue. I thought that was the point of
installing python "as a framework" and then using the macosx backend. I
guess not. The description in the matplotlib docs was misleading.

But thanks, I'll see if I can find a vs-code forum to ask this question.

···

--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

Hey Andy,

Could you point us to where you feel the docs were misleading? Documentation is a difficult, on-going task. So specific and concrete feedback goes a long way towards keeping it useful.

-p

···

On Fri, Feb 7, 2020 at 9:09 AM jandyman andy@voelkel.us wrote:

Yes, I’m aware of the event loop issue. I thought that was the point of

installing python “as a framework” and then using the macosx backend. I

guess not. The description in the matplotlib docs was misleading.

But thanks, I’ll see if I can find a vs-code forum to ask this question.

Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html


Matplotlib-users mailing list

Matplotlib-users@python.org

https://mail.python.org/mailman/listinfo/matplotlib-users

At this link: https://matplotlib.org/faq/usage_faq.html#what-is-a-backend

Under the section talking about what an interactive plot is, I see this:

"Assuming you are running version 1.0.1 or higher, and you have an
interactive backend installed and selected by default, you should see a
plot, and your terminal prompt should also be active; you can type
additional commands such as:"

That sounds like exactly what I want.

Then, at this link: https://matplotlib.org/3.1.0/faq/osx_framework.html

There is a description of using macosx as a framework. So I thought I'd be
good to go.

As a general comment, I didn't really see a good description on the GUI main
loop issue and how various systems deal with it. That would be really handy
to understand when trying to troubleshoot this sort of thing

···

--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

That section assumes the prompt is either the plain python or IPython shell, both of which have machinery to hook up the input_hook below Matplotlib (which is required to have the figure to “be alive”) and we install hooks to trigger re-draws when needed.

In your case you are inside of an input loop in the debugger. For the figure to be live in that context something would need to be registered on the vscode side for the input hook.

It is my understanding that “framework build” in this context refers to how the linking to the system libraries is done.

Tom

···

On Fri, Feb 7, 2020 at 12:44 PM jandyman andy@voelkel.us wrote:

At this link: https://matplotlib.org/faq/usage_faq.html#what-is-a-backend

Under the section talking about what an interactive plot is, I see this:

"Assuming you are running version 1.0.1 or higher, and you have an

interactive backend installed and selected by default, you should see a

plot, and your terminal prompt should also be active; you can type

additional commands such as:"

That sounds like exactly what I want.

Then, at this link: https://matplotlib.org/3.1.0/faq/osx_framework.html

There is a description of using macosx as a framework. So I thought I’d be

good to go.

As a general comment, I didn’t really see a good description on the GUI main

loop issue and how various systems deal with it. That would be really handy

to understand when trying to troubleshoot this sort of thing

Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html


Matplotlib-users mailing list

Matplotlib-users@python.org

https://mail.python.org/mailman/listinfo/matplotlib-users


Thomas Caswell
tcaswell@gmail.com

That is super handy information. It would be great if even those sentences
were included in the docs at the first link I sent:

https://matplotlib.org/faq/usage_faq.html#what-is-a-backend

···

--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

Hi Jandyman,

Hi,

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 got

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 seem

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 wxmplot.

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,

–Matt Newville

···

On Thu, Feb 6, 2020 at 7:41 PM jandyman andy@voelkel.us wrote: