首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pyplot:刷新imshow()窗口

Pyplot:刷新imshow()窗口
EN

Stack Overflow用户
提问于 2016-02-16 10:31:31
回答 1查看 4.6K关注 0票数 2

我有两个方法: generate_window() (显示图像)和on_click() (对显示图像的窗口的单击作出反应)。它们看起来是这样的:

代码语言:javascript
复制
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窗口中显示的图像,但我似乎无法让它工作。我在不同的地方尝试过set_data()和that (),但这没有效果。有什么建议吗?提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-16 12:03:38

您还必须使用plt.ion()启用交互模式,然后在修改后只调用plt.draw()即可。

注意:在使用交互模式时,您必须在block=True上指定参数plt.show(),以防止它立即关闭窗口。

此示例的修改版本应在每次鼠标单击时绘制一个圆圈:

代码语言:javascript
复制
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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35430060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档