# 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")

/home/docs/checkouts/readthedocs.org/user_builds/seaborn-image/envs/latest/lib/python3.8/site-packages/traitlets/traitlets.py:3044: FutureWarning: --rc={'figure.dpi': 96} for dict-traits is deprecated in traitlets 5.0. You can pass --rc <key=value> ... multiple times to add items to a dict.
warn(


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)


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


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")


## Modify colorbar properties¶

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)


### 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)


### 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)


### 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

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)


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())


### Hide colorbar¶

We can hide the colorbar by setting cbar=False

[10]:

ax = isns.imgplot(pol, cbar=False, **scale)


## 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)


## 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


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))


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)