Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Plot disappear when blit is on #17685

Closed
Closed
@Luluser

Description

@Luluser

Already posted on stackoverflow but here should be more appropriate.

Bug report

Bug summary

First, good morning.
As the title describes, my plot disappears when the blitting option is on. I explain a bit further : I am making animations to display the solution of some differential equations and the code will become more and more heavy. I need the blitting option to have smooth animations
and not to "render" the widgets everytime but I also need a button to start/stop the animation. I am using FuncAnimation. The thing is when I stop the animation with the "myAnimation.event_source.stop()" command linked to a widget button, the plot disappears (in some cases the moment my mouse cursor gets out of the pause button, some cases as soon as I press the button). The ax stays empty as long as the animation is on pause and the plot comes back animated when I restart with "myAnimation.event_source.start()".
python_2020-06-19_10-04-11
Code for reproduction
A mock version of the code that is not the code I'm trying to write but simplifies the computations and makes it more readable.

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

class plotanimation:
    def __init__(self):
        self.fig,self.ax=plt.subplots()
        self.x=np.linspace(-10,10,1000)
        self.N=200
        self.interv=50
        self.n0=1./(4*np.pi*2e-4*0.1)**0.5 * np.exp(-self.x**2/(4*2e-4*0.1))  
        self.p,=self.ax.plot(self.x,self.n0)
        self.anim_running = True
        self.Myanimation=animation.FuncAnimation(self.fig, self.update,frames=self.N,interval=self.interv,blit=True)
    def update(self,i):
        self.n0+=i/100
        self.p.set_ydata(self.n0)
        return self.p,
    def animate(self):
        pause_ax = self.fig.add_axes((0.7, 0.025, 0.1, 0.04))
        pause_button = Button(pause_ax, 'pause', hovercolor='0.975')
        pause_button.on_clicked(self._pause)
        plt.show()
    def _pause(self, event):
        if self.anim_running:
            self.Myanimation.event_source.stop()
            self.anim_running = False
        else:
            self.Myanimation.event_source.start()
            self.anim_running = True
    
    
animated_plot = plotanimation()
animated_plot.animate()
# If applicable, paste the console output here
#
#

Expected outcome

The paused plot on the fig. Not an empty ax.

Matplotlib version

  • Operating system: W10
  • Matplotlib version: (3.1.2)
  • Matplotlib backend : TkAgg
  • Python version: v3.8.1:1b293b6
  • Jupyter version (if applicable): None
  • Other libraries: Numpy, scipy

Matplotlib was installed with pip. Conda is not installed.
Thank you a lot.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions