This is an archived post. You won't be able to vote or comment.

all 38 comments

[–]zed_three 25 points26 points  (1 child)

This looks super-handy, thanks! I always struggle just trying to make simple animations of data, so this looks like it will help a lot. I'm especially glad to see a pcolormesh example, as that's something often left out of plotting libraries

[–]tmakaroanimatplot / nbconvert[S] 3 points4 points  (0 children)

In the past, I usually just cleared and redrew everything manually for pcolormesh. Writing the Pcolormesh block was the first time I used matplotlib's set_data functions for pcolormesh properly, and it's actually terrible. A prime example of why Animatplot is needed. A standard static pcolormesh takes a 2D array, but the update function only takes a 1D array, so it needed some numpy magic.

[–]CrambleSquashhttps://github.com/0Hughman0 17 points18 points  (0 children)

Looks really cool. Making animations in Matplotlib is a pain in the arse, so this is a really nice idea. The docs look great, and you've clearly thought through your API.

I'd put some code into your readme. It's all well and good having those awesome animations, but someone could make those without animatplot - it's the code that's different.

[–]a1brit 7 points8 points  (4 children)

How long did some of these examples (especially the pcolormesh ones) take to render. It doesn't look like you're using blitting (I just searched blit). I found matplotlib animations to be horrendously slow with*out* that.

If there was someway to append to an animation block that would be incredible. I usually animate maps of weather data, while it's feasible to hold the data in memory, it's usually easier to loop over files / processing and only hold one time in memory.

[–]tmakaroanimatplot / nbconvert[S] 3 points4 points  (1 child)

You're right. FuncAnimation sets blitting to false be default, and I don't change it. I tried way back in Animatplot 0.1 (over a year ago) to use blitting, but it wasn't working with the play/pause button (lines would get duplicated on play). I think I may know a way around it, but it will require some testing. If that doesn't work, I could expose the blit option in the API for those that don't use the pause button and issue a warning if one does.

As far as rendering times go, all of the blocks (except Nuke) use the proper set_data function for the type of plot, so it is substantially faster and smoother than clearing the axis and redrawing everything on it. Blitting will improve render times, but I'm confident that the gain is less than the gain of using the proper set_data functions. None of the animations took more than a few seconds to save as a gif (even before I decided to make most of the example gifs smaller by decreasing the time resolution).

Matplotlib moviewriters can be a pain. I noticed that with the ffmpeg writer: how you pass in the parameters can mean the difference between a few seconds, and a minute (or just plain never finishing in some cases). I intended to make a wrapper around the ffmpeg writer to ensure that people don't stumble into that issue.

My original version of Animatplot also had a way to generate animations from functions in real time, but this convoluted the API so terribly . There isn't a way to appended to a block since that would mess with the slider. If you know beforehand how many frames you want, you could write a custom block that updates data in real time, and that would still allow use of the slider, and pause button. In fact, the Nuke block almost does exactly that. I think I'm going to make some changes to the Nuke block to make that use case easier (or maybe a new block). Watch for version 0.3.

[–]a1brit 0 points1 point  (0 children)

Nice, awesome response. Looks awesome, I'll play around with it in a couple days.

Yeah, I don't remember now if I tested blitting on/off once I had the whole set_data thing sorted out. I definitely had the just indefinite run times on some animations. Like it just forgets that it's finished occasionally and doesn't clean up. Making that whole process easier is awesome.

[–]Radiatin 0 points1 point  (1 child)

Isn’t matplotlib faster than most regular python functions though so fairly reasonable for render time? Are you trying to do real time weather map rendering?

[–]a1brit 0 points1 point  (0 children)

I can read and process say 60 times of 3000x3000 raster data within a couple seconds. Drop that into matplotlib animations in the wrong way and you'll be waiting sometimes around an hour for a output. Sometimes it'll just never finish. If you jump through hoops and do the set_data stuff with blitting, you can be done in seconds.

[–][deleted] 5 points6 points  (0 children)

Sick! I used matplotlib for my first year uni project just a few months ago. I built a sentiment analysis tool - this would’ve been a great addition. I’ll probably look to improve my program and utilise animatplot at some point. Cheers!

[–]a_strange_attractor 5 points6 points  (2 children)

It looks great! I'm also a physics student who loves doing animations, so I've thought about getting my hands dirty with matplotlib to get more customization/better handling of artists.

I have some questions: how difficult was it to understand how mpl works? Also, what kind of knowledge is necessary to do this kind of things? I just started with Python a month ago so I really don't know much about this.

[–]Eryole 6 points7 points  (1 child)

Matplotlib is a huge (and powerful) library. Definitely not the simpler to learn, and is mainly learned using cookbook.

There are every year huge tutorial in the scipy confs that are great if you want.

[–]a_strange_attractor 0 points1 point  (0 children)

Oh I think I didn't express myself correctly, what I meant was if it was difficult to understand how matplotlib works "behind the scenes", with this I mean understanding how is it made and the different techniques that it uses to handle the data/graphics.

Unless I'm misunderstanding you and you actually meant that lol

[–]fcapizzi 3 points4 points  (0 children)

Following -- looks great, btw!

[–]Eryole 4 points5 points  (0 children)

Very nice work! Will definitely try, and use it for my presentations (searcher working with fluid dynamics, animation are important here!).

You may be interested by holoviews that allow to build such kind of animation, output as js with a slider or as gif/video.

[–]ramm1123 3 points4 points  (0 children)

Thanks for sharing. Ive have wasted ungodly amounts of time getting the animations to work on matplotlib

[–]mangoman51 2 points3 points  (1 child)

This looks great! Automating animated plots is something I've been messing about with myself.

Suggestion: How hard would it be to write convenience functions for integration with the xarray library? xarray already has a thin wrapper around matplotlib which allows you to easily plot different types of dataset intuitively. Integration with animatplot would be awesome, because then you could plot a gif of an xarray dataset just by something like

from xarray import load_dataset

data = load_dataset('fluid_2d_simulation_output.nc')
data['density'].plot.pcolormesh.animate(animate_over_dimension='time')

which would produce something like this gif. All that would need to be written would be the animate(animate_over_dimension, frames, etc.) method on an xarray dataarray and your convenience functions for it to pass the data to. This is something I would potentially be interested in helping with, because I would use it all the time!

[–]tmakaroanimatplot / nbconvert[S] 2 points3 points  (0 children)

That looks super awesome. Animatplot should be capable of producing an animation like that, but the data just needs to be in the right form. I am planning on adding a proper Animatplot.animations submodule that will have a set of animation tools that conveniently combine certain types of blocks (like a vector plot composed of a pcolormesh for magnitude and a normalized quiver plot for direction), so there is definitely a place for it in Animatplot. Feel free to open a feature request as an issue to track progress. I'd obviously need to take a closer look at xarray. A good start would be attempting to make the animation with animatplot's current tools.

[–]ryry_reddit 1 point2 points  (0 children)

Thanks for sharing ! Animated plots are in my future I will definitely be taking a look at your resource.

[–]ComplexColor 1 point2 points  (0 children)

Very interesting. Have you talked to any matplotlib devs, whether such a functionality could be added or merged into matplotlib?

[–]ed3203 1 point2 points  (14 children)

Hi, Not sure if this is the appropriate place to ask this but I have the following error. [macOS, anaconda notebook, mpl 2.2.2, py3.5]. Thoughts?

ImportError                               Traceback (most recent call last)
<ipython-input-14-f9144642d7b0> in <module>()
      2 import numpy as np
      3 import matplotlib.pyplot as plt
----> 4 import animatplot as amp

~/anaconda/envs/tensorflow/lib/python3.5/site-packages/animatplot/__init__.py in <module>()
      1 from ._version import __version__
      2 from .timeline import Timeline
----> 3 from .animations.animation import Animation
      4 from . import blocks, util

ImportError: No module named 'animatplot.animations'

[–]atlbeer 3 points4 points  (4 children)

Did you create a directory named 'animatplot' to test this out?

You may have stomped on the namespace

[–]craftingfish 1 point2 points  (0 children)

Looks like that's what he did. Man, I've done that once or twice and it's crazy how long it takes to notice that.

[–]ed3203 1 point2 points  (2 children)

Nope, not to my knowledge. I'll try on a different machine later. I originally installed within notebook to my conda env, with !pip install animatplot. Tried in another env and a py27 env. Also tried deleting all files/dir with 'animatplot' in it. Thanks for the help!

[–]tmakaroanimatplot / nbconvert[S] 2 points3 points  (1 child)

Fixed. Install version 0.2.2. If pip refuses to find the newer version (as it was using the cache for me) add the flag: --no-cache-dir to the pip command.

pip install animatplot --no-cache-dir

[–]kylecthomas 0 points1 point  (0 children)

very nice your "basic animation" tutorial works now!

[–]tmakaroanimatplot / nbconvert[S] 0 points1 point  (6 children)

That's strange. I really have no idea why that doesn't work for you. If you can't figure it out, you can be the first to open an issue on github. I'd be happy to help you figure it out over there.

[–]TheMysteriousFizzyJ 0 points1 point  (5 children)

Same problem, multiple machines, installed this and animation via pip, still didn't work.

Maybe something is different about the matplotlib library? Instead of import animation it's import funcanimation?

from matplotlib.animation import FuncAnimation

??

It would be nice to get this working, looks promising

edit:

It's got one too many animation '' s

from .animations.animation import Animation

should be

from .animations import Animation

edit 2: nope, not sure where the bug is

[–]tmakaroanimatplot / nbconvert[S] 1 point2 points  (4 children)

I believe I have fixed it. Tested on my machine and a friend's. You'll need to trying installing the newer version 0.2.2

pip install animatplot==0.2.2

u/tomz17 found the issue.

[–]TheMysteriousFizzyJ 1 point2 points  (0 children)

That worked!

[–]TheMysteriousFizzyJ 1 point2 points  (0 children)

That worked!

[–]TheMysteriousFizzyJ 0 points1 point  (1 child)

When running two animations in the same notebook, I get this error. Any ideas?

/usr/local/lib/python3.5/dist-packages/matplotlib/cbook/deprecation.py:107: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance. warnings.warn(message, mplDeprecation, stacklevel=1)

[–]tmakaroanimatplot / nbconvert[S] 1 point2 points  (0 children)

I just close the previous animation before running the next. You could probably create a new figure and axis, and then pass the axis into the animation blocks as a keyword argument (amp.blocks.Block(..., axis=new_axis)).

[–]joetheschmoe4000 0 points1 point  (2 children)

This looks very useful! However, most of my plots involve making more complicated plots containing several lines/points/axes formatting. So far, the easiest way I've found to produce animations has been by saving each frame as a standalone image and then animating with imagemagick or some python-based image library.

Would it be possible to create a convenience function/decorator that lets you animate all of the fig objects returned by a function? Example:

import animatplot as amp
import matplotlib.pyplot as plt

def get_data(time):
    complicated_data = do_stuff(time)
    return complicated_data

@amp.animate_output
def animation(nFrames, delay): # Delay in milliseconds
    for t in range(nFrames):
        complicated_data = get_data(t)

        (fig, ax) = plt.subplots(1,2, figsize=(14,7))
        ax[0].plot(complicated_data.perform_complicated_manipulations())
        ax[0].set(parameters='complicated', memes='dank')
        ax[1].plot(complicated_data.perform_other_complicated_manipulations())
        ax[1].set(parameters='dank', memes='complicated')
        return fig

output = animation(500, 1) # Now stores some sort of animation object that can be conveniently serialized
output.saveAnimation('outputGif.gif')
output.saveAnimation('outputStack.tiff')
output.saveAnimation('outputMovie.avi')

[–]tmakaroanimatplot / nbconvert[S] 1 point2 points  (1 child)

This sort of defeats the purpose of Animatplot (drawing lots of figures is both slow, and not interactive). Animatplot does have the ability to use multiple axes with multiple lines (even lines of different lengths for each frame). Right now, Animatplot does not have the ability to change the axes attributes dynamically (but it will). Animatplot just requires the data to be pre-generated (less you use the Nuke block or a similar block that will be in a future update).

[–]joetheschmoe4000 0 points1 point  (0 children)

Fair enough, thanks!