1

I am adapting this example to interact with a plot using matplotlib's widgets. The way I normally work is interactive, from within spyder I just call a function that does the plotting. My goal is to make an executable available to users who do not have Python installed, so as an intermediate step I am wrapping the functions into a script. I have minimal experience with standalone scripts, so in a nutshell mine looks like this:

import various_modules
def plotting()
    ...
    plot_some_initial_stuff
    sl = Slider()
    plt.show()                <-------
    def update()
       ...
       ax.set_ydata()
       fig.canvas.draw_idle()
    sl.on_change(update)
    return()
plotting()

So I just define the plotting function and then call it. I had to add the plt.show() command, which I do not need to have when I'm working from the IPython shell, otherwise doing:

python my_plot.py

would not produce anything. By adding plt.show(), the window shows up with the graphs I define in the initialization part. However, no interaction happens.

What is the correct way of achieving interaction when running a script like I'm doing?

Community
  • 1
  • 1
Michele Ancis
  • 1,265
  • 4
  • 16
  • 29

1 Answers1

0

Although I do not know all the details, I learnt that:

  • the default mode for script is non-interactive
  • this means that
    • no plot is generated until show() is called
    • the execution of the script does not proceed after the statement

Therefore, my call to show() was too early and actually blocked the definition of the update function and its connection to the slider.on_change() event.

It has been enough to move show() to the last line of the function definition to obtain the desired behaviour.

Michele Ancis
  • 1,265
  • 4
  • 16
  • 29