繁体   English   中英

Matplotlib-使用正值和负值更新条形图

[英]Matplotlib - Updating bar graph with positive and negative values

我正在使用此答案中描述的方法来动态更新条形图。 但是,我想要的条形图应显示来自零点而不是底部的值。 这是我的条形码初始化代码:

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

而且我希望能够从零开始对图表进行正负更新。 但是,使用上面链接中的方法,我只能使它到达图的底部而不是零点的高度。 例如,输入-50应该在0到-50之间绘制条形,但是相反,它是在-100到-50之间绘制条形。

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()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM