1

I am quite new to Python, so please excuse if this is a stupid beginner's error. However I am struggling with it for quite some time. I want to create a figure with n x m subplots, each subplot being np.array of shape [1024,264,264]. As I am looking for differences occuring in the stack along the 0-dimension I want to use a slider to explore all stacks in my figure simultaneously. The slider instance works nicely for a figure with one subplot but I can't bring them all to work. That's the code I am using:

import os
from matplotlib import pyplot as plt
import numpy as np

import glob
import h5py
#Define the xy size of the mapped array
xsize=3
ysize=3

lengthh5=9
readlist=[]
for i in range (0,lengthh5):
    npraw=np.random.rand(200,50,50)
    readlist.append (npraw)

''' Slider visualization'''
from matplotlib.widgets import Slider
fig=plt.figure()
for k in range (0,lengthh5):
    ax=fig.add_subplot(xsize,ysize,k)        
    frame = 10
    l = ax.imshow(readlist[k][frame,:,:]) 
    plt.axis('off')
           sframe = Slider(fig.add_subplot(50,1,50), 'Frame', 0, len(readlist[0])-1, valinit=0)
    def update(val):
        frame = np.around(sframe.val)
        l.set_data(readlist[k][frame,:,:])


sframe.on_changed(update)

plt.show()

For this particular case I stripped it down to a 3x3 array for my figure and just create randmom (smaller) arrays. The slider is interestinly only operable on the second last subplot. However I have no real idea how to link it to all subplots simulatenously. Perhaps someone has an idea how to do this. Thanks a lot in advance,

Tilman

Ed Smith
  • 12,716
  • 2
  • 43
  • 55
Tilman
  • 13
  • 1
  • 4

1 Answers1

1

You need to store each imshow AxesImage in a list and inside update, loop over all of them and update each based on the slider,

import os
from matplotlib import pyplot as plt
from matplotlib.widgets import Slider
import numpy as np

import glob
import h5py
#Define the xy size of the mapped array
xsize=3
ysize=3

lengthh5=9
readlist=[]
for i in range (0,lengthh5):
    npraw=np.random.rand(200,50,50)
    readlist.append (npraw)

fig=plt.figure()
ls = []
for k in range (0,lengthh5):
    ax=fig.add_subplot(xsize,ysize,k)        
    frame = 10
    l = ax.imshow(readlist[k][frame,:,:]) 
    ls.append(l)
    plt.axis('off')

sframe = Slider(fig.add_subplot(50,1,50), 'Frame', 
                0, len(readlist[0])-1, valinit=0)

def update(val):
    frame = np.around(sframe.val)
    for k, l in enumerate(ls):
        l.set_data(readlist[k][frame,:,:])

sframe.on_changed(update)
plt.show()
Ed Smith
  • 12,716
  • 2
  • 43
  • 55
  • That works perfectly. Thanks a lot for the swift help – Tilman Jul 06 '15 at 14:46
  • Sorry for prematurely accepting the answer. I played around with actual data that gave more "features" than random data. There it appears that now I am inserting the 8th image of my array to all the 9 images when I move the slider. The image is changing but it's just the eigths. I guess it's related to the k in the l for loop in the update but I don't have a solution right now. – Tilman Jul 06 '15 at 18:35
  • Yeah sorry, I missed the `k` index on the readlist which should change with axis number too (changed above using enumerate). – Ed Smith Jul 06 '15 at 22:15
  • @EdSmith You added `k` sliders, if you just change `sframe = Slider(fig.add_subplot(50,1+k,50),...` you would notice that, so at the end you are playing with the last plotted slider. If you just remove the intend before that line it works fine. – philippos Jun 30 '17 at 06:50
  • @philippos, intend? I assume you mean indent, good spot, I've adjusted the example. – Ed Smith Jun 30 '17 at 08:49
  • @EdSmith You are right! it's `indent`. sorry my mistyping :) – philippos Jun 30 '17 at 08:51