所有.我想在imagedata更改时更新图形的颜色条.所以类似于:
img = misc.lena()
fig = plt.figure()
ax = plt.imshow(im)
plt.colorbar(ax)
newimg = img+10*np.randn(512,512)
def update_colorbar(fig,ax,newimg):
cbar = fig.axes[1]
ax.set_data(newimg)
cbar.update_normal(ax)
plt.draw()
Run Code Online (Sandbox Code Playgroud)
但似乎fig.axes()的返回结果没有像我预期的颜色条实例.我可以将colorbar实例作为参数传递给更新函数,但我认为只传递一个fig参数可能就足够了.任何人都可以解释一下如何从图中检索颜色条?或者为什么'fig.axes()'不返回AxesImage或Colobar实例,只返回Axes或AxesSubplot?我想我只需要更多地了解Axes/Figure的东西.谢谢!
我想更新contourf函数内的绘图,效果很好。然而,数据的范围发生了变化,因此我还必须更新颜色条。这就是我未能做到的地方。
请参阅以下最小工作示例:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
# Random data
data = np.random.rand(10, 10)
# Plot data
levels = np.linspace(0., 1., 100)
plot = ax.contourf(data, levels=levels)
clist = plot.collections[:]
# Create colorbar
cbar = plt.colorbar(plot)
cbar_ticks = np.linspace(0., 1., num=6, endpoint=True)
cbar.set_ticks(cbar_ticks)
plt.show()
def update():
# Remove old plot
for c in clist:
ax.collections.remove(c)
clist.remove(c)
# Create new data and plot
new_data = 2.*np.random.rand(10, 10)
new_levels = np.linspace(0., 2., …Run Code Online (Sandbox Code Playgroud)