2

I have a list of 2D arrays that I plot them using a slider from matplotlib.
I added some control buttons to my plot, so I want to click a play button, and the plot changes with some time interval.
Here's the function that is called by the button:

def play(mouse_event):    
    for x in range(len(listOfMoments)):
        image.set_data(listOfMoments[x]) 
        time.sleep(0.5)
        print(x)

The print(x) shows that x is increasing normally, however, it plots only the last array of the list, after the increment finishes. My question is: How can I make it plot the array one by one, as it expected to be?

I should mention also that I tried the while loop and got the same results.

1 Answer 1

2

You need to tell the backend to update the figure after changing the data each time through the loop:

fig.canvas.draw()

Here's the documentation reference.

If you don't have a reference to the Figure object, you can probably retrieve it with fig = plt.gcf().

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

2 Comments

That what was missing. I just added fig.canvas.draw() and it worked. Thanks a lot!
You might also use a timer with a callback instead of sleep so that you retain interactivity during the delay. See matplotlib.org/examples/event_handling/timers.html

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.