简体   繁体   English

使用matplotlib滑块小部件更改图像的爬升

[英]Using matplotlib slider widget to change clim in image

I have virtually no experience with python, but I am trying to create a simple script which loads an image and uses the slider widget to adjust the minimum and maximum of the colorbar, and redraws the image data accordingly. 我几乎没有python的经验,但是我试图创建一个简单的脚本来加载图像,并使用滑块控件调整颜色条的最小值和最大值,并相应地重绘图像数据。

I am trying to follow this example: http://matplotlib.sourceforge.net/examples/widgets/slider_demo.html . 我正在尝试遵循以下示例: http : //matplotlib.sourceforge.net/examples/widgets/slider_demo.html I have tried to just change the plot command to imshow, and to use the slider values to set the clim of my image. 我试图将plot命令更改为imshow,并使用滑块值设置图像的清晰度。 However I get the following error message, from the call 'im1,=ax.imshow' (on line 12 in the code below): 但是,我从调用“ im1,= ax.imshow”(在下面的代码的第12行)中收到以下错误消息:

'AxesImage' object is not iterable 'AxesImage'对象不可迭代

I don't understand what this call does, but apparently it can not be used with imshow(). 我不知道此调用的作用,但显然不能与imshow()一起使用。 If I remove the comma in that call, I get no errors, but the image does not update when the sliders are changed. 如果在该调用中删除逗号,则不会出现任何错误,但是更改滑块后图像不会更新。 Does anyone have an alternative solution, or an explanation of why this does not work? 是否有人有替代解决方案,或者对为什么这种方法无效的解释? Any help would be appreciated, thank you. 任何帮助,将不胜感激,谢谢。

My code is as follows: 我的代码如下:

from pylab import *
from matplotlib.widgets import Slider, Button, RadioButtons
import matplotlib.pyplot as plt
import numpy as np

close('all')

ax = subplot(111)
subplots_adjust(left=0.25, bottom=0.25)
min0 = 0
max0 = 25000

im=np.loadtxt('im.txt')
im1,=ax.imshow(im)
colorbar()

axcolor = 'lightgoldenrodyellow'
axmin = axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)
axmax  = axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)

smin = Slider(axmin, 'Min', 0, 30000, valinit=min0)
smax = Slider(axmax, 'Max', 0, 30000, valinit=max0)

def update(val):
    im1.set_clim=(smin.val,smax.val)
    draw()
smin.on_changed(update)
smax.on_changed(update)

show()

Mostly, you just had lots and lots of syntax problems. 通常,您只是有很多语法问题。

For example, you were trying to unpack a single value ( im1,=ax.imshow(im) ) giving you the TypeError you mentioned in your question (as it should). 例如,您尝试解压缩单个值( im1,=ax.imshow(im) ),从而得到您在问题中提到的TypeError (应如此)。 You also set a function to a value when you meant to call it: ( im1.set_clim=(smin.val,smax.val) ). 您还可以在要调用函数时将其设置为一个值:( im1.set_clim=(smin.val,smax.val) )。

Also, I removed the from pylab import * from your example. 另外,我从您的示例中from pylab import *删除了。 This is fine for interactive use, but please, please, please don't use it for actual code. 这对于交互式使用是很好的,但是请,请不要将其用于实际代码。 It makes it very difficult to tell where the functions you're calling are coming from (and the pylab namespace, in particular, is huge by design. It should only be used for interactive use or quick one-use scripts.) 这使得很难知道要调用的函数来自何处(尤其是pylab名称空间在设计上非常庞大只能用于交互式用途或快速的一次性用途脚本。)

Here's a working example (using random data): 这是一个工作示例(使用随机数据):

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Slider, Button, RadioButtons

fig = plt.figure()
ax = fig.add_subplot(111)
fig.subplots_adjust(left=0.25, bottom=0.25)
min0 = 0
max0 = 25000

im = max0 * np.random.random((10,10))
im1 = ax.imshow(im)
fig.colorbar(im1)

axcolor = 'lightgoldenrodyellow'
axmin = fig.add_axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)
axmax  = fig.add_axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)

smin = Slider(axmin, 'Min', 0, 30000, valinit=min0)
smax = Slider(axmax, 'Max', 0, 30000, valinit=max0)

def update(val):
    im1.set_clim([smin.val,smax.val])
    fig.canvas.draw()
smin.on_changed(update)
smax.on_changed(update)

plt.show()

According to the documentation, imshow returns a `matplotlib.image.AxesImage' object. 根据文档, imshow返回一个“ matplotlib.image.AxesImage”对象。 When you put in that comma, Python assumes that the return type of the function is going to be something iterable (usually a tuple when you would be using this construction, but not necessarily) because Python lets you write code like this: 当您输入该逗号时,Python假定该函数的返回类型将是可迭代的(使用此构造时,通常是一个元组,但不一定),因为Python允许您编写如下代码:

a = my_function() # a = (c, d)
a, b = my_function() # a = c, b = d

but

a, = my_function() # you should get an error

I'm not entirely sure what Python is putting in im1 without checking (but from your question I get the impression that writing im1,=... works while im1=... doesn't), but I suspect you are failing to draw the image for some reason. 我不完全确定Python是否在不检查的情况下将其放入了im1 (但是从您的问题中我得到的印象是,编写im1,=...可以工作,而im1=...却不行),但是我怀疑您无法由于某些原因绘制图像。 Does update actually get called? 确实会调用update吗? If it does, maybe try im1.draw() instead. 如果确实如此,则可以尝试使用im1.draw()代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM