簡體   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