Pyplot:刷新 imshow() Window
Pyplot: Refreshing an imshow() Window
我有两种方法:显示图像的 generate_window() 和对显示图像的 window 上的点击做出反应的 on_click() 。它们看起来像这样:
def generate_panel(img):
plt.figure()
ax = plt.gca()
fig = plt.gcf()
implot = ax.imshow(img)
# When a colour is clicked on the image an event occurs
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
def onclick(event):
if event.xdata != None and event.ydata != None:
# Change the contents of the plt window here
在代码的最后一行,我希望能够更改 plt window 中显示的图像,但我似乎无法让它工作。我在不同的地方尝试了 set_data() 和 draw() ,但没有奏效。有什么建议么?提前致谢。
您还必须使用 plt.ion()
启用交互模式
然后在您的修改应该起作用后调用 plt.draw()
。
注意:使用交互模式时,您必须在 plt.show()
上指定参数 block=True
,以防止它立即关闭 window。
你的例子的这个修改版本应该在每次鼠标点击时绘制一个圆圈:
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
import numpy as np
def generate_panel(img):
plt.figure()
ax = plt.gca()
fig = plt.gcf()
implot = ax.imshow(img)
# When a colour is clicked on the image an event occurs
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show(block=True)
def onclick(event):
if event.xdata is not None and event.ydata is not None:
circle = plt.Circle((event.xdata,
event.ydata), 2, color='r')
fig = plt.gcf()
fig.gca().add_artist(circle)
plt.draw()
# Change the contents of the plt window here
if __name__ == "__main__":
plt.ion()
img = np.ones((600, 800, 3))
generate_panel(img)
我有两种方法:显示图像的 generate_window() 和对显示图像的 window 上的点击做出反应的 on_click() 。它们看起来像这样:
def generate_panel(img):
plt.figure()
ax = plt.gca()
fig = plt.gcf()
implot = ax.imshow(img)
# When a colour is clicked on the image an event occurs
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
def onclick(event):
if event.xdata != None and event.ydata != None:
# Change the contents of the plt window here
在代码的最后一行,我希望能够更改 plt window 中显示的图像,但我似乎无法让它工作。我在不同的地方尝试了 set_data() 和 draw() ,但没有奏效。有什么建议么?提前致谢。
您还必须使用 plt.ion()
启用交互模式
然后在您的修改应该起作用后调用 plt.draw()
。
注意:使用交互模式时,您必须在 plt.show()
上指定参数 block=True
,以防止它立即关闭 window。
你的例子的这个修改版本应该在每次鼠标点击时绘制一个圆圈:
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
import numpy as np
def generate_panel(img):
plt.figure()
ax = plt.gca()
fig = plt.gcf()
implot = ax.imshow(img)
# When a colour is clicked on the image an event occurs
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show(block=True)
def onclick(event):
if event.xdata is not None and event.ydata is not None:
circle = plt.Circle((event.xdata,
event.ydata), 2, color='r')
fig = plt.gcf()
fig.gca().add_artist(circle)
plt.draw()
# Change the contents of the plt window here
if __name__ == "__main__":
plt.ion()
img = np.ones((600, 800, 3))
generate_panel(img)