我有一个处理图像的循环,我希望在每 100 次迭代时使用 matplotlib 在单个输出窗口中显示图像。所以我试图编写一个函数,它将采用 numpy 张量作为输入并显示相应的图像。
这是我所拥有的不起作用的内容:
def display(image):
global im
# If im has been initialized, update it with the current image; otherwise initialize im and update with current image.
try:
im
im.set_array(image)
plt.draw()
except NameError:
im = plt.imshow(image, cmap=plt.get_cmap('gray'), vmin=0, vmax=255)
plt.show(block=False)
plt.draw()
我一开始试图通过 FuncAnimation 传递它,但这似乎是为了让动画调用函数来进行更新,而不是在 matplotlib 上调用函数来显示结果。
上面的代码打开一个窗口,但它似乎没有更新。有人能在这里指出正确的方向吗?
非常感谢,
Justin
也许您可以组合使用:
-
图.canvas.draw_idle()
and
- plt.暂停()
第一个将重新绘制图形,而第二个将调用 GUI 事件循环来更新图形。
另外,您不需要每次都调用 imshow,在“im”对象上调用“set_data”方法就足够了。类似的东西应该有效:
import matplotlib.pyplot as plt
import numpy
fig,ax = plt.subplots(1,1)
image = numpy.array([[1,1,1], [2,2,2], [3,3,3]])
im = ax.imshow(image)
while True:
image = numpy.multiply(1.1, image)
im.set_data(image)
fig.canvas.draw_idle()
plt.pause(1)
这是改编自this回答。希望能帮助到你。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)