Matplotlib:在imshow中的set_data对图没有影响 [英] Matplotlib: no effect of set_data in imshow for the plot

查看:36
本文介绍了Matplotlib:在imshow中的set_data对图没有影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的错误,没有你的帮助我无法修复.在matplotlib中使用 imshow 设置图像后,即使使用方法 set_data 对其进行了更改,它也始终保持不变.看看这个例子:

I have a strange error which I can't fix without your help. After I set an image with imshow in matplotlib it stays the same all the time even if I change it with the method set_data. Just take a look on this example:

import numpy as np
from matplotlib import pyplot as plt


def newevent(event):
    haha[1,1] += 1
    img.set_data(haha)
    print img.get_array()    # the data is change at this point
    plt.draw()

haha = np.zeros((2,2))
img = plt.imshow(haha)
print img.get_array()        # [[0,0],[0,0]]
plt.connect('button_press_event', newevent)
plt.show()

绘制完图后,方法 set_data 不会改变图中的任何内容.有人可以解释一下为什么吗?

After I plot it, the method set_data doesn't change anything inside the plot. Can someone explain me why?

编辑

仅添加了几行以指出我实际想要做的事情.我想在按下鼠标按钮后重绘数据.我不想删除整个数字,因为如果只有一件事发生变化,那将是愚蠢的.

Just added a few lines to point out what I actually want to do. I want to redraw the data after I press a mouse button. I don't want to delete the whole figure, because it would be stupid if only one thing changes.

推荐答案

问题是因为你在第一次调用后没有更新像素缩放.

The problem is because you have not updated the pixel scaling after the first call.

当您实例化 imshow 时,它会根据初始数据设置 vminvmax,并且不再接触它.在您的代码中,它将 vmin vmax 都设置为0,因为您的数据 haha​​ = zeros((2,2))是到处都是零.

When you instantiate imshow, it sets vmin and vmax from the initial data, and never touches it again. In your code, it sets both vmin and vmax to 0, since your data, haha = zeros((2,2)), is zero everywhere.

您的新事件应该包括使用 img.autoscale() 进行自动缩放,或者通过将 img.norm.vmin/vmax 设置为您喜欢的内容来明确设置新的缩放条件.

Your new event should include autoscaling with img.autoscale() , or explicitly set new scaling terms by setting img.norm.vmin/vmax to something you prefer.

设置新的vminvmax的函数是:

img.set_clim(vmin=new_vim, vmax=new_vmax)

这篇关于Matplotlib:在imshow中的set_data对图没有影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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