Matplotlib animate fill_between form

I am trying to animate the fill_between form inside matplotlib and I don't know how to update the PolyCollection data. Take this simple example: I have two lines, and I always fill in between them. Of course, the lines change and are animated.

Here is an example of a mannequin:

import matplotlib.pyplot as plt # Init plot: f_dummy = plt.figure(num=None, figsize=(6, 6)); axes_dummy = f_dummy.add_subplot(111); # Plotting: line1, = axes_dummy.plot(X, line1_data, color = 'k', linestyle = '--', linewidth=2.0, animated=True); line2, = axes_dummy.plot(X, line2_data, color = 'Grey', linestyle = '--', linewidth=2.0, animated=True); fill_lines = axes_dummy.fill_between(X, line1_data, line2_data, color = '0.2', alpha = 0.5, animated=True); f_dummy.show(); f_dummy.canvas.draw(); dummy_background = f_dummy.canvas.copy_from_bbox(axes_dummy.bbox); # [...] # Update plot data: def update_data(): line1_data = # Do something with data line2_data = # Do something with data f_dummy.canvas.restore_region( dummy_background ); line1.set_ydata(line1_data); line2.set_ydata(line2_data); # Update fill data too axes_dummy.draw_artist(line1); axes_dummy.draw_artist(line2); # Draw fill too f_dummy.canvas.blit( axes_dummy.bbox ); 

The question is how to update fill_between Poly values ​​based on line1_data and line2_data every time update_data () is called and draws them before blit ("# Update fill data too" and "# Draw fill too"). I unsuccessfully executed fill_lines.set_verts () and could not find an example ...

Thanks!

+7
source share
3 answers

Well, as someone pointed out that we are dealing with a collection here, so we will have to delete and redraw

for this axis object axes_dummy = f_dummy.add_subplot (111);

so somewhere in update_data (): you will ..

delete all collections associated with it

 for coll in (axes_dummy.collections): axes_dummy.collections.remove(coll) 

draw a new "fill_between" Polycollection

  axes_dummy.fill_between(x,y-sigma,y+sigma, facecolor='yellow', alpha=0.5) 

A similar trick is necessary for overlaying an unfilled contour graph on top of a filled one, since an unfilled contour graph is also an assembly (from the lines that I assume?)

+3
source

This is not my answer, but I found it most useful:

http://matplotlib.1069221.n5.nabble.com/animation-of-a-fill-between-region-td42814.html

Hi Mauricio, Patch objects are a bit more difficult to work with line objects, because unlike line objects, this is a step remote from the input provided by the user. Here is an example similar to what you want to do here: http://matplotlib.org/examples/animation/histogram.html

Basically, you need to change the top of the path in each frame. It might look something like this:

 from matplotlib import animation import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.set_xlim([0,10000]) x = np.linspace(6000.,7000., 5) y = np.ones_like(x) collection = plt.fill_between(x, y) def animate(i): path = collection.get_paths()[0] path.vertices[:, 1] *= 0.9 animation.FuncAnimation(fig, animate, frames=25, interval=30) 

Take a look at path.vertices to see how they are laid out. Hope this helps, Jake

+1
source

If you do not want to use anitmation or remove everything from your figure in order to update only the filling, you can use my method:

call fill_lines.remove() and then call axes_dummy.fill_between() again to draw new ones. Worked for me.

+1
source

All Articles