1

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?

3
  • What do you want to see changing in the animation between one frame and the next? Commented Aug 17, 2021 at 8:00
  • basically the histograms bars Commented Aug 17, 2021 at 8:15
  • Please refer to the official reference for histogram animation. Commented Aug 17, 2021 at 8:16

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

amazing, exactly what I needed, for some reason its very slow on my jupyter notebook, I suppose thats due to my compute instance
It could be the compute instance; however you can make an attempt by reducing the interval = 100 parameter, which determines the interval in milliseconds between two consecutive frames
this is weird, on my coursera jupyter notebook environment, I see the animation, however in my Azure ML environment I just see a blank image with no bars at all. am I missing something ?
I use daily PyCharm and Jupyter Notebook and I know they works fine. I honestly don't know Azure ML, so I may suggest you to post an other question asking why the animation does not play on Azure but works fine on notebook
it works now, I was missing %matplotlib noteboook line on top

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.