Matplotlib 图未随数据更改而更新 [英] Matplotlib figure not updating on data change

查看:83
本文介绍了Matplotlib 图未随数据更改而更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 matplotlib 实现一个图像查看器.这个想法是对图像所做的更改(例如过滤器应用程序)将自动更新.

I'm implementing an image viewer using matplotlib. The idea is that changes being made to the image (such as filter application) will update automatically.

我创建了一个图以显示初始图像,并使用 pyQt 添加了一个按钮来更新数据.数据确实发生了变化,我已经检查过,但图中没有.但是,如果在按下过滤器应用程序按钮后,我使用 matplotlib 的标准工具栏移动图像,则该图像为更新.

I create a Figure to show the inital image and have added a button using pyQt to update the data. The data does change, I have checked, but the Figure does not. However, if after I've pressed the filter application button, I move the image using matplotlib's standard tool bar, the image is then updated.

我认为在更新图像时我做错了什么,但是由于移动图像实际上会强制更新,因此它会显示数据更改.不过,我希望当我按下按钮时会发生这种情况.

I assume I'm doing something wrong when updating the image, but since the fact of moving it actually forces the update, it then shows the data change. I would like for this to happen when I press the button, though.

下面是一些代码.这是初始图形初始化,显示原始图像:

Below is some of the code. This is the initial figure initialization, which shows the original image:

    self.observableFig = Figure((4.0, 4.0), dpi=100)
    self.canvas = FigureCanvas(self.observableFig)
    self.canvas.setParent(self.observableWindow)
    self.canvas.setFocusPolicy(Qt.StrongFocus)
    self.canvas.setFocus()

    self.canvas.mpl_connect('button_press_event', self.on_click)

    # Showing initial data on Window
    self.observableFig.clear()
    self.observableAxes = self.observableFig.add_subplot(1, 1, 1)
    min, max = self.min, self.max
    self.observableAxes.imshow(self.data, vmin=min, vmax=max, origin='lower') 

这是按下更改数据的按钮时的事件:

And this is the event for when the button that changes the data is pressed:

    self.observableAxes.imshow(self.data/2, origin='lower')
    #    plt.clf()
    #    plt.draw()
    #    plt.show()

我已经尝试过 draw() show(),基本上我在 pyplot 上找到的与此有关的所有内容.一开始我也尝试过使用和不使用 plt.ion() ,但在这方面没有任何区别.

I have tried draw(), show(), basically anything I've found on pyplot about this. I have also tried both with and without plt.ion() at the beginning, but it hasn't made a difference in this.

谢谢.

推荐答案

什么都没有更新的原因是,您试图对不属于pyplot的图形使用 pyplot 方法状态机. plt.draw()不会绘制该图形,因为 plt 不知道该图形存在.

The reason that nothing is updating is that you're trying to use pyplot methods for a figure that's not a part of the pyplot state machine. plt.draw() won't draw this figure, as plt doesn't know the figure exists.

使用 fig.canvas.draw() 代替.

无论如何,最好使用 fig.canvas.draw()而不是 plt.draw(),因为很明显您在绘制哪个图形(前者绘制的是一个,后者绘制所有内容,但前提是它们被 pyplot 跟踪.

Regardless, it's better to use fig.canvas.draw() that plt.draw(), as it's clear which figure you're drawing (the former draws one, the latter draws all, but only if they're tracked by pyplot).

尝试以下方法:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.random((10,10))

# To make a standalone example, I'm skipping initializing the 
# `Figure` and `FigureCanvas` and using `plt.figure()` instead...
# `plt.draw()` would work for this figure, but the rest is identical.
fig, ax = plt.subplots()
ax.set(title='Click to update the data')
im = ax.imshow(data)

def update(event):
    im.set_data(np.random.random((10,10)))
    fig.canvas.draw()

fig.canvas.mpl_connect('button_press_event', update)
plt.show()

这篇关于Matplotlib 图未随数据更改而更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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