Luis Valencia
Luis Valencia

Reputation: 33998

Matplot lib animation not working as expected

I have 4 variables like this:

# generate 4 random variables from the random, gamma, exponential, and uniform distributions
x1 = np.random.normal(-2.5, 1, 10000)
x2 = np.random.gamma(2, 1.5, 10000)
x3 = np.random.exponential(2, 10000)+7
x4 = np.random.uniform(14,20, 10000)

And I need to create one figure with 4 subplots.

so I tried this:

plt.figure(figsize=(9,3))
plt.subplot(1,4,1)
plt.hist(x1, normed=True, bins=20, alpha=0.5)

plt.subplot(1,4,2)
plt.hist(x2, normed=True, bins=20, alpha=0.5)

plt.subplot(1,4,3)
plt.hist(x3, normed=True, bins=20, alpha=0.5)

plt.subplot(1,4,4)
plt.hist(x4, normed=True, bins=20, alpha=0.5)
plt.axis([-7,21,0,0.6])

And I got this result

enter image description here

Now I want to create an animation on the subplots, so I did the following (trying one subplot only)

import matplotlib.animation as animation


def update(curr):
    if curr == n:
        a.event_source.stop()
    plt.cla()
    plt.figure(figsize=(9,3))
    plt.subplot(1,4,1)
    plt.hist(x1, normed=True, bins=20, alpha=0.5)
    
    
    plt.axis([-7,21,0,0.6])
    plt.gca().set_title('Sample')
    plt.gca().set_ylabel('Frequency')
    plt.gca().set_xlabel('Value')
    plt.annotate('n = {}'.format(curr), [3.27])

fig  = plt.figure()
a = animation.FuncAnimation(fig, update, interval=100)

However the end result is empty, nothing is shown.

Any idea?

Upvotes: 1

Views: 60

Answers (1)

Zephyr
Zephyr

Reputation: 12496

I re-structured your code in order to plot the animation of the 4 subplots. Without any specific indication on what you want to see changing between one frame and the next, I assume the number of sample drawn from each distribution is inscreasing in each frame by 10.

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


def update(curr):

    N = 10*curr
    x1 = np.random.normal(-2.5, 1, N)
    x2 = np.random.gamma(2, 1.5, N)
    x3 = np.random.exponential(2, N) + 7
    x4 = np.random.uniform(14, 20, N)

    ax[0].cla()
    ax[0].hist(x1, bins = 20, alpha = 0.5, color = 'blue', edgecolor = 'blue')
    ax[0].set_title('Normal')
    ax[0].set_ylabel('Frequency')
    ax[0].set_xlabel('Value')
    ax[0].set_xlim(-6, 1)


    ax[1].cla()
    ax[1].hist(x2, bins = 20, alpha = 0.5, color = 'blue', edgecolor = 'blue')
    ax[1].set_title('Gamma')
    ax[1].set_ylabel('Frequency')
    ax[1].set_xlabel('Value')
    ax[1].set_xlim(0, 12)


    ax[2].cla()
    ax[2].hist(x3, bins = 20, alpha = 0.5, color = 'blue', edgecolor = 'blue')
    ax[2].set_title('Exponential')
    ax[2].set_ylabel('Frequency')
    ax[2].set_xlabel('Value')
    ax[2].set_xlim(7, 25)


    ax[3].cla()
    ax[3].hist(x4, bins = 20, alpha = 0.5, color = 'blue', edgecolor = 'blue')
    ax[3].set_title('Uniform')
    ax[3].set_ylabel('Frequency')
    ax[3].set_xlabel('Value')
    ax[3].set_xlim(14, 20)

    ax[0].set_ylim(0, 250)
    fig.suptitle(f'Number of samples: {N}')

    plt.tight_layout()


fig, ax = plt.subplots(1, 4, figsize = (9, 3), sharey = 'all')
a = FuncAnimation(fig, update, interval = 100, frames = 81)

plt.show()

enter image description here

Upvotes: 2

Related Questions