如何使用 python 的 matplotlib 重绘图像? [英] how do I redraw an image using python's matplotlib?

查看:243
本文介绍了如何使用 python 的 matplotlib 重绘图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做的事情似乎很简单,但是我花了一点时间试图使其工作.我只是尝试使用 imshow 绘制图像,然后在新数据到达时定期重新绘制.

What I am trying to do seems to be fairly straightforward, but I'm having a heck of a time trying to get it to work. I am simply trying to draw an image using imshow and then re-draw it periodically as new data arrives.

我从这里开始:

fig = figure()
ax = plt.axes(xlim=(0,200),ylim=(0,200))
myimg = ax.imshow(zeros((200,200),float))

然后我假设可以像这样调用set_data来更新图像:

Then I'm assuming I can call set_data like this to update the image:

myimg.set_data(newdata)

我尝试了许多其他事情,例如我改称 ax.imshow(newdata),或者在之后尝试使用 figure.show()set_data().

I've tried many other things, for example I've called ax.imshow(newdata) instead or I've tried using figure.show() after set_data().

推荐答案

每次向图形添加新内容时,您都可以简单地调用fig.canvas.draw().这将刷新情节.

You can simply call figure.canvas.draw() each time you append something new to the figure. This will refresh the plot.

from matplotlib import pyplot as plt
from builtins import input

fig = plt.figure()
ax = fig.gca()
fig.show()

block = False
for i in range(10):
    ax.plot(i, i, 'ko')
    fig.canvas.draw()
    if block: 
        input('pause : press any key ...')
    else:
        plt.pause(0.1)
plt.close(fig)

这篇关于如何使用 python 的 matplotlib 重绘图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆