Problem animating a histogram and movie at the same time

I am having a problem animating a histogram and movie at the same time
using matplotlib.animation.ArtistAnimation.

The first program below, which makes an animated histogram using the "bar"
routine works fine. If I substitute the two lines commented out for the
two lines preceding those lines, it still works fine (using "plot" instead
of 'bar"). For reasons that are not clear to me, I need make a
list (ims) for animation for the histogram using the "bar" routine, and a
list of lists for animation of the simple "plot" routine.

There is a more serious problem with the second routine where I
simultaneously animate a sequence of images using imshow and also try to
make an animated histogram. It works fine if I plot the "histogram" using
the "plot" routine but does not work if I use the "bar" routine. Can
someone offer some help?

I am running these on OSX 10.12.5, Python 3.5.2 or Python 3.6.1, and
matplotlib 2.0.0 or 2.0.2

···

----------------------------

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

fig, ax = plt.subplots()

ims = []
xh = np.random.randn(10)
for i in range(100):
    xh = np.append(xh, np.random.randn(10))
    a, b = np.histogram(xh, bins=24, normed=True)
    xx = 0.5*(b[:-1]+b[1:])
    ax.set_xlim(-3, 3)
    ax.set_ylim(0, 0.5)
    im = ax.bar(xx, a, width=0.9*(b[1]-b[0]), color='C1')
    ims.append(im)
    # im, = ax.plot(xx, a, '-oC0')
    # ims.append([im])

ani = animation.ArtistAnimation(fig, artists=ims, interval=50,
                                repeat=False)
plt.show()

----------------------------

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

def f(x, y):
    return np.sin(x) + np.cos(y)

xm = np.linspace(0, 2 * np.pi, 120)
ym = np.linspace(0, 2 * np.pi, 120).reshape(-1, 1)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 3.5))
ax1.axis('off')

ims = []
xh = np.random.randn(10)
for i in range(100):
    xm += np.pi / 20.
    ym += np.pi / 20.
    im1 = ax1.imshow(f(xm, ym), cmap=plt.get_cmap('plasma'),
                     aspect='equal', animated=True)

    xh = np.append(xh, np.random.randn(10))
    a, b = np.histogram(xh, bins=24, normed=True)
    xx = 0.5*(b[:-1]+b[1:])
    ax2.set_xlim(-3, 3)
    ax2.set_ylim(0, 0.5)
    # im2 = ax2.bar(xx, a, width=0.9*(b[1]-b[0]), color='C1')
    im2, = ax2.plot(xx, a, '-oC0')
    ims.append([im1, im2])

ani = animation.ArtistAnimation(fig, artists=ims, interval=50,
                                repeat=False)
# ani.save('junk.mp4')
plt.show()

----------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20170614/2c3e5a42/attachment.html>

David,

Thanks for the complete examples, it made figuring this out really quick.
ArtistAnimation expects to be given a list of lists (or tuples), where the
inner collection contains all of the artists that should be rendered for a
given frame. In the case of bar, it returns a BarCollection object (which I
just learned), is a subclass of tuple. This explains why it works (by
itself), when directly appended to the list given to ArtistAnimation; the
BarCollection acts as the collection of artists that ArtistAnimation is
expecting. In the case of the second example, ArtistAnimation is being
given a list([BarCollection, Image]); because BarCollection isn't actually
an Artist, it causes the problem. What you should try is:

ims.append([im1] + list(im2))

This converts the BarCollection to a list of the individual Bar artists,
and adds to the list containing the image; ArtistAnimation can understand
this and it seems to work fine on my system.

Ryan

···

On Wed, Jun 14, 2017 at 1:43 PM, David J Pine <djpine at gmail.com> wrote:

I am having a problem animating a histogram and movie at the same time
using matplotlib.animation.ArtistAnimation.

The first program below, which makes an animated histogram using the "bar"
routine works fine. If I substitute the two lines commented out for the
two lines preceding those lines, it still works fine (using "plot" instead
of 'bar"). For reasons that are not clear to me, I need make a
list (ims) for animation for the histogram using the "bar" routine, and a
list of lists for animation of the simple "plot" routine.

There is a more serious problem with the second routine where I
simultaneously animate a sequence of images using imshow and also try to
make an animated histogram. It works fine if I plot the "histogram" using
the "plot" routine but does not work if I use the "bar" routine. Can
someone offer some help?

I am running these on OSX 10.12.5, Python 3.5.2 or Python 3.6.1, and
matplotlib 2.0.0 or 2.0.2

--
Ryan May
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20170614/232e5864/attachment.html&gt;