In [1]:
%matplotlib inline
In [2]:
import h5py
import matplotlib.pyplot as plt
import numpy as np
import ipywidgets
plt.rcParams["figure.figsize"] = (12, 10)
We're going to load the data in using h5py from an hdf5 file. HDF5 is a file format that allows for very simple storage of numerical data; in this particular case, we'll be loading in a 3D array, and then examining it.
In [3]:
f = h5py.File("/srv/nbgrader/data/koala.hdf5", "r")
print(list(f.keys()))
Here, we load in the data by reading from the key koala
that we just found.
In [4]:
koala = f["/koala"][:]
print(koala.shape)
We'll use subplots to show the maximum value along each of the three axes, along with a histogram of all the values. The .max()
function here accepts and axis
argument, which means "max along a given axis."
In [5]:
for i in range(3):
plt.subplot(2,2,i+1)
plt.imshow(koala.max(axis=i), interpolation='nearest', origin='lower', cmap='viridis')
plt.subplot(2,2,4)
plt.hist(koala.ravel(), bins = 32, log = True)
Out[5]:
We'll make a slicer, too -- this one is along the x value. Note how we take a floating point value and turn that into an index to make the image.
In [6]:
def xslicer(coord = 0.5):
# We're accepting a float here, so we convert that into the right index we want
ind = int(coord * koala.shape[0])
plt.imshow(koala[ind,:,:], interpolation = 'nearest', origin='lower')
In [7]:
ipywidgets.interact(xslicer, coord = (0.0, 1.0, 0.01))
Out[7]: