Image visualization#

In this tutorial, we will see how to use seaborn-image to perform some basic descriptive 2-D image visualization. Throughout this tutorial, we will use imgplot(), an axes-level function. imgplot() provides a lot of functionalities to handle and display different kinds of images.

In addition to imgplot(), we will also use load_image() and set_context() functions.

[1]:
import seaborn_image as isns

isns.set_context("notebook")

We will first take a look at a sample polymer image using the imgplot() function.

[2]:
pol = isns.load_image("polymer")

ax = isns.imgplot(pol)
../_images/Tutorial_Image_Visualization_3_0.svg

We can get brief statistical description of our data by setting the describe parameter to True. Under the hood, imgplot() uses scipy.stats to provide the statistics.

[3]:
ax = isns.imgplot(pol, describe=True)
No. of Obs. : 65536
Min. Value : -8.2457214
Max. Value : 43.714034999999996
Mean : 7.456410761947062
Variance : 92.02680396572863
Skewness : 0.47745180538933696
../_images/Tutorial_Image_Visualization_5_1.svg

Add a scalebar#

So far our image doesn’t tell us anything about the physical feature size in our polymer image. The easiest way to achieve this is by adding a scalebar to the image.

In imgplot(), we simply need to specify the size-per-pixel or dx and the units to add a scalebar. Here, we know the size-per-pixel is 15 nm.

[4]:
ax = isns.imgplot(pol, dx=15, units="nm")
../_images/Tutorial_Image_Visualization_8_0.svg

Modify colorbar properties#

Add colorbar label#

We can add a colorbar label by specifying cbar_label parameter in the imgplot() function.

[5]:
scale = {"dx" : 15, "units" : "nm"}

ax = isns.imgplot(pol, cbar_label="Height (nm)", **scale)
../_images/Tutorial_Image_Visualization_11_0.svg

Change colorbar ticks#

We can modify colorbar ticks by specifying them as cbar_ticks parameter.

[6]:
ax = isns.imgplot(pol, cbar_ticks=[0, 15, 30], **scale)
../_images/Tutorial_Image_Visualization_13_0.svg

Change colorbar orientation#

By specifying the orientation parameter as ‘h’/’horizontal’ or ‘v’/’vertical’, we can change the colorbar orientation

[7]:
ax = isns.imgplot(pol, orientation="h", **scale)
../_images/Tutorial_Image_Visualization_15_0.svg

Log scale colorbar and colormap#

In imgplot(), we can easily change the colormap and colorbar to log scale setting cbar_log=True.

Note: Since imgplot() is an axes-level function, it can be used to plot on an already existing matplotlib figure. You may also use ImageGrid to plot a collection of images. However, note that ImageGrid is a figure-level function (see ImageGrid examples and tutorial).

[8]:
import matplotlib.pyplot as plt

pl = isns.load_image("fluorescence")

f, axes = plt.subplots(figsize=(10,5), nrows=1, ncols=2)

ax0 = isns.imgplot(pl, ax=axes[0])
ax1 = isns.imgplot(pl, ax=axes[1], cbar_log=True)
../_images/Tutorial_Image_Visualization_17_0.svg

Alternatively, you can also use the norm parameter to scale the data.

[9]:
import matplotlib.colors as colors

ax = isns.imgplot(pl, norm=colors.LogNorm())
../_images/Tutorial_Image_Visualization_19_0.svg

Hide colorbar#

We can hide the colorbar by setting cbar=False

[10]:
ax = isns.imgplot(pol, cbar=False, **scale)
../_images/Tutorial_Image_Visualization_21_0.svg

Modify colormaps#

We can customize the look of our image by changing the colormap. All matplotlib colormaps can be used in addition to the colormaps that come with seaborn-image. Colormaps can be changed using the cmap parameter.

Note : Whenever possible, the parameter names in seaborn-image functions are kept the same as in matplotlib functions.

[11]:
ax = isns.imgplot(pol, cmap="ice", **scale)
../_images/Tutorial_Image_Visualization_23_0.svg

Correct for outliers#

Often, the data collected may not be perfect. Outlier data points can easily throw off the entire colormap during visualization. imgplot() provides an easy way to work with such data - by simply setting the robust parameter True.

By default, robust=True plots the 2nd and the 98th percentile of the data. The percentiles can be further tuned using the perc parameter.

[12]:
pol_out = isns.load_image("polymer outliers")

f, axes = plt.subplots(figsize=(10,5), nrows=1, ncols=2)

ax0 = isns.imgplot(pol_out, ax=axes[0])
ax1 = isns.imgplot(pol_out, robust=True, perc=(0.5,99.5), ax=axes[1])

# NOTE: you may want to use `ImageGrid` to plot a collection of images - see examples and tutorial for `ImageGrid`
../_images/Tutorial_Image_Visualization_25_0.svg

The robust parameter recalculates the colormap range and appropriately extends the colorbar. We can further tune the robust beahvior and the colormap rescaling using the vmin and vmax paramter. By default, imgplot() prioritizes the vmin and the vmax parameter over the robust parameter.

[13]:
ax = isns.imgplot(pol_out, vmin=-8, robust=True, perc=(0,99.99))
../_images/Tutorial_Image_Visualization_27_0.svg

Here, specifying the vmin parameter ignores the robust option for the lower value. imgplot() handles the colorbar extension accordingly. The same is true when vmax is specified. If both vmin and vmax are specified, robust parameter is completely ignored.

Despine image axes#

We can despine the image and the colorbar axes by setting the despine parameter True.

[14]:
ax = isns.imgplot(pol, despine=True, **scale)
../_images/Tutorial_Image_Visualization_30_0.svg