Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save_animation on 2 axis simultaneously #188

Open
patquem opened this issue Apr 29, 2021 · 6 comments
Open

save_animation on 2 axis simultaneously #188

patquem opened this issue Apr 29, 2021 · 6 comments
Labels

Comments

@patquem
Copy link

patquem commented Apr 29, 2021

Hello,

How to create a gif animation of 2 subplots iterating simultaneously ?
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

image

@patquem patquem added the enhancement New feature or request label Apr 29, 2021
@patquem
Copy link
Author

patquem commented Apr 29, 2021

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 ianhi added question and removed enhancement New feature or request labels Apr 29, 2021
@ianhi
Copy link
Collaborator

ianhi commented Apr 29, 2021

Hi @patquem

I saw your question on discourse as well so linking that here: https://discourse.matplotlib.org/t/mpl-interactions-save-animation-on-2-axis-simultaneously/22077/

The relevant section of the docs is https://mpl-interactions.readthedocs.io/en/stable/examples/Usage-Guide.html#Only-Using-some-of-the-parameters-+-specifying-axes

In your case you need to do:

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)

@patquem
Copy link
Author

patquem commented Apr 29, 2021

Hi @ianhi ,
Excellent. I missed this :(
Thanks you very much

@ianhi
Copy link
Collaborator

ianhi commented Apr 29, 2021

I'm sorry the docs weren't more clear! I think they definitely could be improved to make stuff like this easier to find.

If you have any suggestions on what would have made this easier for you to find this feel free to let me know :)

@patquem
Copy link
Author

patquem commented Apr 29, 2021

Don't apologize.
The documentation is very clear (with numerous examples/illustrations).
I should have had to pass more time on it.

@ianhi
Copy link
Collaborator

ianhi commented Apr 29, 2021

One other thing to note here is that it looks like you're mostly using mpl-interactions to index through a pre made array right? There are two easy ways to do that that don't involve a function:

Use the indexer convenience function (https://mpl-interactions.readthedocs.io/en/stable/autoapi/mpl_interactions.indexer.html?highlight=indexer#mpl_interactions.indexer). In your case it should be indexer(amplitude, 'k', -1)

  1. If you put the k axis as the zero axis then you can use hyperslicer which knows how to automatically create sliders for indexing through stacks of images: https://mpl-interactions.readthedocs.io/en/stable/examples/hyperslicer.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants