简体   繁体   English

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

[英]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.

相关问题 堆积条形图使用python matplotlib正面和负面值 - Stacked bar charts using python matplotlib for positive and negative values 更新 matplotlib 条形图? - Updating a matplotlib bar graph? 如何使用 Python 显示值构建从正百分比到负百分比的水平堆积条形图 - How to build a horizontal stacked bar graph from positive to negative percentages with values displayed using Python XlsxWriter 中的条形着色以正/负值为条件 - Bar colouring in XlsxWriter conditioned on positive/negative values 带有用于正值和负值的单独条形的条形图 - Bar chart with separate bars for positive and negative values 在轴 matplotlib 上将负值设置为正值以获得百分比 - Setting negative values as positive values on axis matplotlib for percentages Matplotlib条形图轴值 - Matplotlib bar graph axes values 通过在matplotlib中使用Latex,xticklabel中的正值和负值之间的差异 - Difference between positive and negative values in xticklabel by using Latex in matplotlib 使用具有 2 种自定义颜色的 Matplotlib 在散点图中设置正值和负值的颜色 - Setting Colors For Positive and Negative Values in Scatter Plot with Matplotlib with 2 Custom Colors 在Matplotlib中的条形图上显示负值的问题 - Issues in displaying negative values on bar chart in Matplotlib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM