Hello,
How to create a gif animation of 2 subplots iterating simultaneously with mpl_interactions ?
Is there a way to have a ‘global’ controler for both axis ?
(in the example below, only the first axis iterates due to save_animation linked to the 1rst axis controler).
Any idea ?
Thanks.
Patrick
import mpl_interactions.ipyplot as iplt
k = np.arange(128)
def ampli2d(k):
return amplitude[…, k]
def phase2d(k):
return phase[…, k]
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
controls1 = iplt.imshow(ampli2d, k=k, ax=ax1)
controls2 = iplt.imshow(phase2d, k=k, ax=ax2)
anim = controls1.save_animation(“plot-2.gif”, fig, “k”, interval=35, N_frames=100)
Going back to the primary function ‘FuncAnimation’ seems to be a good and simple solution (in this case) :
from future import division
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation, PillowWriter
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(18, 6))
ax1.set_title(‘Amplitude’)
ax1.set_xlabel(xlabel)
ax1.set_ylabel(ylabel)
ax2.set_title(‘Phase’)
ax2.set_xlabel(xlabel)
ax2.set_ylabel(ylabel)
img1 = ax1.imshow(amplitude[…, 0], cmap=plt.get_cmap(‘cool’), vmin=ampli_min, vmax = ampli_max, extent=extent)
img2 = ax2.imshow(phase[…, 0], cmap=plt.get_cmap(‘spring’), vmin=phase_min, vmax = phase_max, extent=extent)
def plot(k):
img1.set_array(amplitude[…, k])
img2.set_array(phase[…, k])
return [img1, img2]
anim = FuncAnimation(fig, plot, 128, blit=True)
anim.save(“movie2.gif”, writer=PillowWriter(fps=24))
plt.show()
ianhi
April 29, 2021, 1:47pm
3
Hi @patquem
Great question
You can pass the first controls
object to the second iplt
function like this:
def ampli2d(k):
return amplitude[…, k]
def phase2d(k):
return phase[…, k]
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
controls = iplt.imshow(ampli2d, k=k, ax=ax1)
_ = iplt.imshow(phase2d, ax=ax2, controls=controls) # <- THIS IS THE IMPORTANT CHANGE
anim = controls.save_animation(“plot-2.gif”, fig, “k”, interval=35, N_frames=100)
I’m sorry that’s not more clear in the docs (if you have any suggestions on how to improve the clarity of that they’d be super welcome!)
Of course using FuncAnimation
as you did is also a very good solution - especially if you don’t need the sliders.
ianhi
April 29, 2021, 1:48pm
4