Skip to content

Instantly share code, notes, and snippets.

@spaghetti-source
Created March 22, 2014 02:27
Show Gist options
  • Save spaghetti-source/9700195 to your computer and use it in GitHub Desktop.
Save spaghetti-source/9700195 to your computer and use it in GitHub Desktop.
animated bar graph
# -*- coding: UTF-8 -*-
#
# source: http://www.mext.go.jp/b_menu/toukei/001/022/2003.htm
#
import random
import matplotlib
import matplotlib.pyplot as plt
import math
y = [53.14, 52.44, 54.13, 54.63, 54.40, 54.85, 51.04, 50.35, 49.29, 47.95, 47.05, 45.28, 42.62, 39.70, 36.86]
z = [6.83, 7.41, 7.74, 7.87, 7.56, 6.29, 6.99, 6.83, 6.49, 6.27, 6.52, 6.16, 6.52, 6.83, 7.00]
x = range(len(y))
def doit():
yt = [ a+b for (a,b) in zip(y,z) ]
r = [ random.random() * 2 * 3.1415 for k in x ]
plt.xticks(x, ["14", "15", "16", "17", "18", "19", "20-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54", "55-59", "60-64"])
rects = plt.bar(x, yt, align = 'center')
for i in range(720):
for k in x:
yt[k] = y[k] + z[k] * math.sin(r[k] + math.radians(i))
for rect, h in zip(rects, yt):
rect.set_height(h)
fig.canvas.draw()
fig = plt.figure()
win = fig.canvas.manager.window
win.after(100, doit)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment