How to plot an ipywidget as a subplot

18 Views Asked by At

I am trying to run an ipywidget as a subplot. This is all in a jupyter notebook. The idea is to have a set of 4 subplots total, one of which is controlled by a slider.

%matplotlib inline
import matplotlib.pyplot as plt
from ipywidgets import interact
from ipywidgets import Play

# define function to create the subplot
def browse_images_subplot(images_array,times,ax):
    n = images_array.shape[0]
    vmin = np.nanpercentile(images_array,5)
    vmax = np.nanpercentile(images_array,95)
    def view_image(Radar_Image=0):
        # plt.figure(figsize=(8,6))
    
        ax.set_title(f'Slideshow test! image number: {Radar_Image} \nDate: {times[Radar_Image].date()}')
        ax.set_xlabel('Easting')
        ax.set_ylabel('Northing')
    
        ax.imshow(images_array[Radar_Image,:,:],vmin=vmin,vmax=vmax)
        
    interact(view_image, Radar_Image=(0,n-1))

# plot 'browse_images_subplot' as part of a subplot

fig, axs = plt.subplots(2,2)
fig.suptitle('Attempt to use browse_images as a subplot')

# call the function and try to plot. 

browse_images_subplot(datasets[0],times,axs[0,0])

The below is what the subplot of interest should look like. This was made using an earlier version of the 'browse_images' function. 'datasets[0]' is an image stack (3D array).

slideshow test

I end up getting this:

subplot test

How do I get the slideshow inside the subplot?

Thanks!

For completeness, I've included the 'browse_images' function that works:

def browse_images(images_array,times):
    n = images_array.shape[0]
    vmin = np.nanpercentile(images_array,5)
    vmax = np.nanpercentile(images_array,95)
    def view_image(Radar_Image=0):
        plt.figure(figsize=(8,6))
    
        plt.title(f'Slideshow test! image number: {Radar_Image} \nDate: {times[Radar_Image].date()}')
        plt.xlabel('Easting')
        plt.ylabel('Northing')
    
        plt.ylim(images_array.shape[1],0) # attempt to deal with spasm from creating each image; didn't work!
    
        plt.imshow(images_array[Radar_Image,:,:],vmin=vmin,vmax=vmax)
        plt.colorbar()
    
    interact(view_image, Radar_Image=(0,n-1))
0

There are 0 best solutions below