越来越多的matplotlib条形图

时间:2014-01-22 12:51:09

标签: python charts 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 个答案:

答案 0 :(得分: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()

答案 1 :(得分:1)

您添加了highcharts标记。因此,在highcharts中,您可以使用每个系列的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