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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.