2014-01-22 99 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