2014-01-22 100 views
1

我有一个条形图中matplotlib是这样的:成长matplotlib条形图

import numpy as np 
import matplotlib.pyplot as plt 

position = np.arange(6) + .5 

plt.tick_params(axis = 'x', colors = '#072b57') 
plt.tick_params(axis = 'y', colors = '#072b57') 

rects = plt.bar(position, (0, 0, 0, 0, 0, 0), align = 'center', color = '#b8ff5c') 
plt.xticks(position, ('A', 'B', 'C', 'D', 'E', 'F')) 

plt.xlabel('X Axis', color = '#072b57') 
plt.ylabel('Y Axis', color = '#072b57') 
plt.title('My Chart', color = '#072b57') 

plt.grid(True) 
plt.show() 

和所有图表的水平是0,但现在我想在我的图表每列从0增长有不同的速度,直到他们都达到其例如为100 例如,在运行应用程序的图表中的最大值会是这样: enter image description here

和剩余的列仍然增长,直到他们都达到最大值,然后该程序将完成。

现在我想问问matplotlib中有什么可以做这个工作吗?

回答

2

是的,你可以在matplotlib中做动画,你需要使用matplotlib.animation查看this blog的介绍。接下来你怎么能够做你想做的事:

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


fig = plt.figure() 

position = np.arange(6) + .5 

plt.tick_params(axis = 'x', colors = '#072b57') 
plt.tick_params(axis = 'y', colors = '#072b57') 

speeds = [1, 2, 3, 4, 1, 2] 
heights = [0, 0, 0, 0, 0, 0] 
rects = plt.bar(position, heights, align = 'center', color = '#b8ff5c') 
plt.xticks(position, ('A', 'B', 'C', 'D', 'E', 'F')) 

plt.xlabel('X Axis', color = '#072b57') 
plt.ylabel('Y Axis', color = '#072b57') 
plt.title('My Chart', color = '#072b57') 

plt.ylim((0,100)) 
plt.xlim((0,6)) 

plt.grid(True) 

rs = [r for r in rects] 

def init(): 
    return rs 

def animate(i): 
    global rs, heights 
    if all(map(lambda x: x==100, heights)): 
     heights = [0, 0, 0, 0, 0, 0] 
    else: 
     heights = [min(h+s,100) for h,s in zip(heights,speeds)] 
    for h,r in zip(heights,rs): 
     r.set_height(h) 
    return rs 

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True) 

plt.show() 
+0

谢谢,这真棒(: – Haiku

1

您添加了关联标签。因此,在高层建筑中,您可以通过每个系列的animation属性来完成。我制成3串联,每一个杆,改变动画速度上的每个:

series: [{ 
    name: 'Tokyo', 
    data: [100, 0, 0], 
    animation: { 
     duration: 1000 
    } 
}, { 
    name: 'NYC', 
    data: [0, 100, 0], 
    animation: { 
     duration: 3000 
    } 
}, { 
    name: 'Berlin', 
    data: [0, 0, 100], 
    animation: { 
     duration: 7000 
    } 
}] 

jsFiddle