Tuesday, 7 June 2022

Matplotlib: Can't Manipulate plot while script is waiting for `input()`

I am trying to get user input via input(), after the user manipulates a plot using the standard zoom controls. Eg. User plays with the plot, figures out the desired X-value, and types it into the command-line prompt.

Plot can be either in a separate window (Spyder/Python) or in-line (in Jupiter Notebook).

After the user types in the value, the script continues (eg. asks for another value from the plot, or does some calculation with the values).

However, I can't get the plot to actually display and be responsive while the command-line is waiting for user-input. I have tried:

  • plot() statement first, input() statement second.
  • Spyder with Python 3.6 (I think), from source via MacPorts (updated Spyder as far as I could)
  • Spyder via Python 3.7 from ContinuumIO's Anaconda package, in IPython
  • Jupiter Notebook also from Anaconda
  • Numerous backends: macosx, qt, etc.
  • Notebook %matplotlib, notebook, inline, qt etc.
  • separate figure windows (Spyder & Python) vs. in-line figures (Jupyter Notebook)
  • fig.show( block=False ) and variations of this, eg. plt.show( block=False )
  • two different MacBooks (2017 and 2010 MacBook Pro's)

I did get the plot to actually update (previously it was either a blank space in a Notebook, or a blank separate figure window) by adding a matplotlib.pyplot.pause(0.5) between the plot() and input() statements. This was major progress, but once the script hits the input() statement, I get a spinning beachball on the Figure window (preventing zooming etc.) until I complete the input() statement by entering something, and then the script completes. At that point the plot is interactive.

It seems like the python console(s) can't handle more than one user-interaction simultaneously? Ie. input() is freezing all other user-interactivity?

I've been searching SO, google etc. for days now and haven't figured this out! The idea was to use this as a "quick and dirty" way to get user input from the plot, prior to undertaking the theoretically more complex task of acquiring user-clicks directly from the plot (which would have to snap to plotted data like data cursors).



from Matplotlib: Can't Manipulate plot while script is waiting for `input()`

No comments:

Post a Comment