[Matplotlib-users] Problem animating a histogram and movie at the same time

David J Pine djpine at gmail.com
Wed Jun 14 15:43:12 EDT 2017


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>


More information about the Matplotlib-users mailing list