使用matplotlib滑块小部件更改图像的爬升 [英] Using matplotlib slider widget to change clim in image

查看:25
本文介绍了使用matplotlib滑块小部件更改图像的爬升的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎没有使用python的经验,但是我试图创建一个简单的脚本来加载图像,并使用滑块控件调整颜色条的最小值和最大值,并相应地重新绘制图像数据.

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.

我正在尝试遵循以下示例:http://matplotlib.sourceforge.net/示例/小部件/slider_demo.html.我试图将plot命令更改为imshow,并使用滑块值设置图像的清晰度.但是,我从调用"im1,= ax.imshow"(在下面的代码的第12行)中收到以下错误消息:

I am trying to follow this example: 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. However I get the following error message, from the call 'im1,=ax.imshow' (on line 12 in the code below):

'AxesImage' 对象不可迭代

'AxesImage' object is not iterable

我不明白这个调用是做什么的,但显然它不能与 imshow() 一起使用.如果删除该调用中的逗号,则不会出现任何错误,但更改滑块时图像不会更新.有没有人有替代解决方案,或解释为什么这不起作用?任何帮助将不胜感激,谢谢.

I don't understand what this call does, but apparently it can not be used with 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.

我的代码如下:

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.

例如,您尝试解压缩单个值( im1,= ax.imshow(im)),从而得到问题中提到的 TypeError (如这应该).当您打算调用它时,您还将函数设置为一个值:(im1.set_clim=(smin.val,smax.val)).

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). You also set a function to a value when you meant to call it: (im1.set_clim=(smin.val,smax.val)).

此外,我从您的示例中删除了 from pylab import * .这对于交互式使用是很好的,但是请,请不要将其用于实际代码.很难知道要调用的函数来自何处(特别是pylab名称空间在设计上巨大.应该只使用 用于交互式使用或快速的一次性脚本.)

Also, I removed the from pylab import * from your example. 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.)

这是一个工作示例(使用随机数据):

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()

这篇关于使用matplotlib滑块小部件更改图像的爬升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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