在 matplotlib 中动态更新堆积条形图
Dynamically updating a stacked bar plot in matplotlib
我想知道如何在 matplotlib 中动态更新堆积条形图。
这个问题 Dynamically updating a bar plot in matplotlib 描述了如何对普通条形图而不是堆叠条形图进行操作。
在普通条形图中,假设 rects = plt.bar(range(N), x, align='center')
,可以通过 rect.set_height(h)
完成更新
但是在堆叠条形图中我们还需要设置底部。
p2 = plt.bar(ind, womenMeans, width, color='y',
bottom=menMeans, yerr=menStd)
如何动态设置底部?不幸的是,'Rectangle' 对象似乎没有属性 'set_bottom'。有没有其他方法可以解决这个问题?
出于某种原因,您想要的 set_bottom()
函数是 bar
中 return 对象中 patches
下的 set_y
。基于您建议的 link 的最小示例看起来像
import numpy as np
import matplotlib.pyplot as plt
def setup_backend(backend='TkAgg'):
import sys
del sys.modules['matplotlib.backends']
del sys.modules['matplotlib.pyplot']
import matplotlib as mpl
mpl.use(backend) # do this before importing pyplot
import matplotlib.pyplot as plt
return plt
N = 5
width = 0.35 # the width of the bars: can also be len(x) sequence
def animate():
# http://www.scipy.org/Cookbook/Matplotlib/Animations
mu, sigma = 100, 15
h = mu + sigma * np.random.randn((N*2))
p1 = plt.bar(np.arange(N), h[:N], width, color='r')
p2 = plt.bar(np.arange(N), h[N:], width, color='b', bottom=h[:N])
assert len(p1) == len(p2)
maxh = 0.
for i in range(50):
for rect1, rect2 in zip(p1.patches, p2.patches):
h = mu + sigma * np.random.randn(2)
#Keep a record of maximum value of h
maxh = max(h[0]+h[1],maxh)
rect1.set_height(h[0])
rect2.set_y(rect1.get_height())
rect2.set_height(h[1])
#Set y limits to maximum value
ax.set_ylim((0,maxh))
fig.canvas.draw()
plt = setup_backend()
fig, ax = plt.subplots(1,1)
win = fig.canvas.manager.window
win.after(10, animate)
plt.show()
注意,我每次迭代都使用随机数更改高度生成,因此可以压缩两个补丁数组(否则会有点乱)。
我想知道如何在 matplotlib 中动态更新堆积条形图。
这个问题 Dynamically updating a bar plot in matplotlib 描述了如何对普通条形图而不是堆叠条形图进行操作。
在普通条形图中,假设 rects = plt.bar(range(N), x, align='center')
rect.set_height(h)
完成更新
但是在堆叠条形图中我们还需要设置底部。
p2 = plt.bar(ind, womenMeans, width, color='y',
bottom=menMeans, yerr=menStd)
如何动态设置底部?不幸的是,'Rectangle' 对象似乎没有属性 'set_bottom'。有没有其他方法可以解决这个问题?
出于某种原因,您想要的 set_bottom()
函数是 bar
中 return 对象中 patches
下的 set_y
。基于您建议的 link 的最小示例看起来像
import numpy as np
import matplotlib.pyplot as plt
def setup_backend(backend='TkAgg'):
import sys
del sys.modules['matplotlib.backends']
del sys.modules['matplotlib.pyplot']
import matplotlib as mpl
mpl.use(backend) # do this before importing pyplot
import matplotlib.pyplot as plt
return plt
N = 5
width = 0.35 # the width of the bars: can also be len(x) sequence
def animate():
# http://www.scipy.org/Cookbook/Matplotlib/Animations
mu, sigma = 100, 15
h = mu + sigma * np.random.randn((N*2))
p1 = plt.bar(np.arange(N), h[:N], width, color='r')
p2 = plt.bar(np.arange(N), h[N:], width, color='b', bottom=h[:N])
assert len(p1) == len(p2)
maxh = 0.
for i in range(50):
for rect1, rect2 in zip(p1.patches, p2.patches):
h = mu + sigma * np.random.randn(2)
#Keep a record of maximum value of h
maxh = max(h[0]+h[1],maxh)
rect1.set_height(h[0])
rect2.set_y(rect1.get_height())
rect2.set_height(h[1])
#Set y limits to maximum value
ax.set_ylim((0,maxh))
fig.canvas.draw()
plt = setup_backend()
fig, ax = plt.subplots(1,1)
win = fig.canvas.manager.window
win.after(10, animate)
plt.show()
注意,我每次迭代都使用随机数更改高度生成,因此可以压缩两个补丁数组(否则会有点乱)。