DEV Community

Cover image for Attractive, Effective & Descriptive Image Visualization in Python
SarthakJariwala
SarthakJariwala

Posted on

Attractive, Effective & Descriptive Image Visualization in Python

Introducing seaborn-image for image visualization in python

Alt Text

This blog post introduces some of the functionalities in seaborn-image for descriptive, effective and attractive image visualization.

Seaborn-image is an open source python visualization library for images built on top of matplotlib. It aims to provide a high level API to visualize image data similar to how seaborn provides high level API to visualize tabular data. As the name suggests, seaborn-image is heavily inspired by the seaborn library.

Let's begin by installing seaborn-image

$ pip install --upgrade seaborn-image
Enter fullscreen mode Exit fullscreen mode

and then importing seaborn-image as isns

All the functions in seaborn-image are available in a flat namespace.

import seaborn_image as isns

# set context 
isns.set_context("notebook")

# set global image settings
isns.set_image(cmap="deep", origin="lower")

# load sample dataset
polymer = isns.load_image("polymer")
Enter fullscreen mode Exit fullscreen mode

isns.set_context() helps us globally change the display contexts (similar to seaborn.set_context()).

In addition to the context, we also globally set properties for drawing our image using isns.set_image(). Later, we will also look at globally setting image scalebar properties using isns.set_scalebar().

Under the hood, these functions use matplotlib rcParams for customizing displays. You can refer to the docs for more details on settings in seaborn-image.

Lastly, we load a sample polymer dataset from seaborn-image.

2-D images using imgplot()

Visualizing the image is as simple as calling the imgplot() function with the image data. imgplot() uses matplotlib imshow under the hood but provides easy access to a lot of customizations. We will take a look at a few of the customizations in this blog post.

By default, it adds a colorbar and turns off axis ticks. However, that is only beginning to scratch the surface!

We can get some basic descriptive statistics about our image data by setting describe=True.

ax = isns.imgplot(
    polymer,
    describe=True,  # default is False
)
Enter fullscreen mode Exit fullscreen mode
No. of Obs. : 65536
Min. Value : -8.2457214
Max. Value : 43.714034999999996
Mean : 7.456410761947062
Variance : 92.02680396572863
Skewness : 0.47745180538933696
Enter fullscreen mode Exit fullscreen mode

2d-image-viz

Draw a scalebar

Although we know some basic information about our image data, we still do not have any information about the physical size of the features in the image. We can draw a scalebar to rectify it.

To add a scalebar to the image we can specify the individul pixel size dx and physical units. Here, the individual pixel is 15 nanometers in physical size. So, we set dx=15 and units="nm".

ax = isns.imgplot(
    polymer,
    dx=15,  # physical size of the pixel
    units="nm",  # units 
    cbar_label="Height (nm)"  # colorbar label to our image
)
Enter fullscreen mode Exit fullscreen mode

image-with-scalebar

Note: We only specified the individual pixel size and units, and a scalebar of appropriate size was drawn.

Tip: You can change the scalebar properties such as scalebar location, label location, color, etc. globally using isns.set_scalebar()

Correct for outliers

Data is rarely perfect. It is often riddled with outliers and these outliers affect the image display.

# sample data with outliers
pol_outliers = isns.load_image("polymer outliers")

ax = isns.imgplot(pol_outliers, cbar_label="Height (nm)")
Enter fullscreen mode Exit fullscreen mode

img-with-outliers

The above example dataset has a single outlier pixel which is affecting the image display. We can correct for the outliers using the robust parameter in all seaborn-image functions.

ax = isns.imgplot(
    pol_outliers,
    robust=True,  # set robust plotting
    perc=(0.5, 99.5),  # set the percentile of the data to view
    cbar_label="Height (nm)"
)
Enter fullscreen mode Exit fullscreen mode

outlier-corrected-image

Here, we are setting robust=True and plotting 0.5 to 99.5 percentile of the data (specified using the perc parameter). Doing so appropriately scales the colormap based on the robust percentile specified and also draws colorbar extensions without any additional code.

Note: You can specify the vmin and vmax parameter to override the robust parameter. See imgplot documentation examples for more details

Image data distribution - imghist()

One of the most important things in image visualization is knowing the distribution of the underlying image data. Here, we are using imghist() to plot a histogram along with the image.

fig = isns.imghist(polymer, dx=15, units="nm", cbar_label="Height (nm)")
# note that there are no new parameters required
Enter fullscreen mode Exit fullscreen mode

image-with-histogram

Using histogram along with an appropriate colormap provides additional information about the image data. For instance, from the histogram above, we can see that majority of the data has values less than 30 nm and there are very few values that are close to 40 nm - something that may not be obvious if we look at the image without the histogram.

Tip: You can change the number of bins using the bins parameter and the orientation of the colorbar and histogram using the orientation parameter. See imghist documentation examples for more details

Importantly, generating the entire figure - with a histogram matching the colorbar levels, scalebar describing the physical size of the features in the image, colorbar label, hiding axis ticks, etc. - took only one line of code. In essense, this is what seaborn-image aims to provide - attractive, descriptive and effective image visualization.

Lastly, this post has only introduced some of the high level API that seaborn-image provides for image visualization. In later posts, we will take a look at other functionalities such as easily transforming and visualizing image data, working with RGB images as well as visualizing multi-dimensional images.


For more details, you can check out the detailed documentation and tutorials and the project on GitHub.

Top comments (0)