[英]Matplotlib - Updating bar graph with positive and negative values
I am using the method described in this answer to dynamically update a bar graph. 我正在使用此答案中描述的方法来动态更新条形图。 The bar graph I want however should show values coming from the zero-point rather than the bottom of the graph. 但是,我想要的条形图应显示来自零点而不是底部的值。 This is my bar initialisation code: 这是我的条形码初始化代码:
oxBar = aBar.bar(0, 200, bottom=-100, width=1)
And I want to be able to update my graph positively and negatively from the zero point. 而且我希望能够从零开始对图表进行正负更新。 However using the method in the link above I can only get it to go to a height from the bottom of the graph rather than the zero-point. 但是,使用上面链接中的方法,我只能使它到达图的底部而不是零点的高度。 For example, inputting -50 should draw the bar from 0 to -50, however instead it is drawing the bar from -100 to -50 instead. 例如,输入-50应该在0到-50之间绘制条形,但是相反,它是在-100到-50之间绘制条形。
Matplotlib
as default autosizes plot but you can set ylim
to have constant size/height and then 0
can be always in middle. Matplotlib
是默认的自动调整大小图,但是您可以将ylim
设置为恒定的大小/高度,然后始终将0
居中。
Example 例
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.