2016-11-23 94 views
0

我使用this answer中描述的方法来动态更新条形图。然而,我想要的条形图应该显示来自零点而不是图底部的值。这是我的酒吧初始化代码:Matplotlib - 使用正值和负值更新条形图

oxBar = aBar.bar(0, 200, bottom=-100, width=1) 

而且我希望能够从零点积极和消极地更新我的图形。然而,使用上面链接中的方法,我只能将它从图的底部移到高度,而不是零点。例如,输入-50应该从0到-50绘制小节,但是反而是将小节从-100绘制为-50。

+0

为什么不使用'底部= 0'? 'plt.bar(0,-50,bottom = 0,width = 1)'? – furas

+0

我可以,但是我想从图的中点开始绘制图形的原点,并且使用此方法始终从底部开始,所以这不能解决问题。 –

+0

但它从0到-50 - 即。 'plt.bar([0,1,2],[-100,-50,100],bottom = 0,width = 1)'从0到-100,从0到100,0在中间。可能您需要其他属性来设置绘图大小。 – furas

回答

1

Matplotlib作为默认自动调整绘图,但您可以将ylim设置为具有恒定大小/高度,然后0可始终处于中间。

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
import random 

def animate(frameno): 
    x = random.randint(-200, 200) 
    n = [x, -x] 

    for rect, h in zip(rects, n): 
     rect.set_height(h) 

    return rects 

# --- init --- 

fig, ax = plt.subplots() 
rects = plt.bar([0,1], [0,0], width=1) 
plt.ylim([-200, 200]) 

ani = animation.FuncAnimation(fig, animate, blit=True, 
           interval=100, frames=100, repeat=False) 
plt.show() 

enter image description here