使用 matplotlib 滑块小部件更改图像中的 clim

2022-01-18 00:00:00 python matplotlib widget

问题描述

我几乎没有使用 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.我试图将绘图命令更改为 imshow,并使用滑块值来设置图像的 clim.但是,我从调用 '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 命名空间,在设计上是 huge.它应该只被使用用于交互式使用或快速一次性脚本.)

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

相关文章