如何使用python的matplotlib重新绘制图像?

时间:2022-09-02 23:21:22

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.

我正在尝试做的事情似乎相当简单,但我花了很多时间试图让它工作。我只是尝试使用imshow来绘制图像,然后在新数据到来时定期重新绘制。

I've started out with this:

我从这个开始:

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

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

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

myimg.set_data(newdata)

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().

我尝试过很多其他的东西,例如我调用了ax.imshow(newdata),或者我尝试过在set_data()之后使用figure.show()。

1 个解决方案

#1


10  

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

每次你给这个数字添加一些新东西时,你都可以简单地调用“图”。canvas.draw()。这将使情节焕然一新。

from matplotlib import pyplot as plt

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

for i in range(10):
    ax.plot(i, i, 'ko')
    f.canvas.draw()
    raw_input('pause : press any key ...')
f.close()

#1


10  

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

每次你给这个数字添加一些新东西时,你都可以简单地调用“图”。canvas.draw()。这将使情节焕然一新。

from matplotlib import pyplot as plt

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

for i in range(10):
    ax.plot(i, i, 'ko')
    f.canvas.draw()
    raw_input('pause : press any key ...')
f.close()