Using the matplotlib slider widget to change a clip in an image

I have practically no experience with python, but I'm trying to create a simple script that loads an image and uses a slider widget to set the minimum and maximum color bar and redraw the image data accordingly.

I am trying to follow this example: http://matplotlib.sourceforge.net/examples/widgets/slider_demo.html . I tried just changing the plot command to imshow and using the values ​​of the slider to set the click of my image. However, I get the following error message: "im1, = ax.imshow" (line 12 in the code below):

AxesImage is not iterable

I do not understand what this call makes, but apparently it cannot be used with imshow (). If I remove the comma in this call, I will not get any errors, but the image will not be updated when the sliders change. Does anyone have an alternative solution or explanation why this is not working? Any help would be appreciated, thanks.

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() 
+6
source share
2 answers

Basically, you had a lot of syntax issues.

For example, you tried to unpack a single value ( im1,=ax.imshow(im) ) by specifying the TypeError that you mentioned in your question (as it should be). You also set the function to the value when you wanted to name it: ( im1.set_clim=(smin.val,smax.val) ).

Also, I removed from pylab import * from your example. This is normal for interactive use, but please, please do not use it for actual code. It's hard to tell where the functions you are calling come from (and the pylab namespace, in particular, is huge in design, it should only be used for interactive use or quick one-time scripts.)

Here is 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() 
+7
source

According to the documentation, imshow returns a matplotlib.image.AxesImage object. When you insert this comma, Python assumes that the return type of the function will be something iterable (usually a tuple when you use this construct, but not necessarily), because Python allows you to write code as follows:

 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 quite sure that Python puts im1 without checking (but from your question, the impression is that the im1,=... record works, but im1=... not), but I suspect that you cannot for some reason reason we draw the image. Is update called? If so, try im1.draw() instead.

+1
source

Source: https://habr.com/ru/post/885605/


All Articles