1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from random import uniform, randint
fig = plt.figure(figsize=(8,6), dpi=80) # figsize in dots, dpi (dot per inch) so it result size is (8*80,6*80).
subplot = plt.subplot(1,1,1) # Getting the subplot.
def update(arg) :
# Animation update screen function:
# generate differents bars at every call.
global subplot
pos=[]
datas=[]
colors=[]
for v in range(0,24) :
# Generate the datas for the bars.
pos.append(v)
datas.append(randint(5,24))
colors.append( (uniform(0.0,1.0), uniform(0.0,1.0), uniform(0.0,1.0)) )
# Set the bars and keep the returned value for function returning.
ret=subplot.bar(pos, datas, width=1.0, animated=True, color = colors, fill=True ) # Color.
subplot.set_xticks([]) # X axes values marks.
subplot.set_yticks([]) # Y axes values marks.
return ret # Must be returned so that the screen is cleaned.
def init_function() :
# Animation initialisation function:
# called one time at animation start.
global subplot
pos=[]
datas=[]
colors=[]
for v in range(0,24) :
# Generate the datas for the bars.
pos.append(v)
datas.append(randint(5,24))
colors.append( (uniform(0.0,1.0), uniform(0.0,1.0), uniform(0.0,1.0)) )
# Set the bars and keep the returned value for function returning.
ret=subplot.bar(pos, datas, width=1.0, animated=True, color = colors, fill=True ) # Color.
subplot.set_xticks([]) # X axes values marks.
subplot.set_yticks([]) # Y axes values marks.
return ret # Must be returned so that the screen is cleaned.
animation=FuncAnimation(fig , update, interval=100, blit=True, frames=200, init_func=init_function)
plt.show() |
Partager