Testing (and demonstrating) plot_images()


In [2]:
# %hyperspy -r inline
import numpy as np
import hyperspy.api as hs
%matplotlib inline
import matplotlib.pyplot as plt

plot_images() is used to plot several images in the same figure. It supports many configurations and has many options available to customize the resulting output. The function returns a list of matplotlib axes, which can be used to further customize the figure. Some examples are given below.

Default usage

A common usage for plot_images() is to view the different slices of a multidimensional image (a hyperimage):


In [3]:
import scipy.ndimage
image = hs.signals.Image(np.random.random((2, 3, 512, 512)))
for i in range(2):
    for j in range(3):
        image.data[i,j,:] = scipy.misc.ascent()*(i+0.5+j)
        
axes = image.axes_manager
axes[2].name = "x"
axes[3].name = "y"
axes[2].units = "nm"
axes[3].units = "nm"
    
image.metadata.General.title = 'multi-dimensional Lena'
hs.plot.plot_images(image, tight_layout=True)


/home/fjd29/Anaconda/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_base.py:1057: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  if aspect == 'normal':
/home/fjd29/Anaconda/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_base.py:1062: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  elif aspect in ('equal', 'auto'):
Out[3]:
[<matplotlib.axes._subplots.AxesSubplot at 0x7fac2cf12290>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabd9367e90>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabd928b510>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabd91a0850>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabd903d990>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabd8f5b090>]

Specified labels

By default, plot_images() will attempt to auto-label the images based on the Signal titles. The labels (and title) can be customized with the label and suptitle arguments. In this example, the axes labels and ticks are also disabled with axes_decor:


In [4]:
import scipy.ndimage
image = hs.signals.Image(np.random.random((2, 3, 512, 512)))
for i in range(2):
    for j in range(3):
        image.data[i,j,:] = scipy.misc.ascent()*(i+0.5+j)
        
axes = image.axes_manager
axes[2].name = "x"
axes[3].name = "y"
axes[2].units = "nm"
axes[3].units = "nm"
    
image.metadata.General.title = 'multi-dimensional Lena'
hs.plot.plot_images(image, suptitle='Custom figure title', 
                       label=['Image 1', 'Image 2', 'Image 3', 'Image 4', 'Image 5', 'Image 6'],
                       axes_decor=None, tight_layout=True)


Out[4]:
[<matplotlib.axes._subplots.AxesSubplot at 0x7fabd8f797d0>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabd81a9b10>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabd80dca50>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabd06aaa10>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabd0557290>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabd0424b50>]

List of images

plot_images() can also be used to easily plot a list of Images, comparing different Signals, including RGB images. This example also demonstrates how to wrap labels using labelwrap (for preventing overlap) and using a single colorbar for all the Images, as opposed to multiple individual ones:


In [5]:
import scipy.ndimage

# load red channel of raccoon as an image
image0 = hs.signals.Image(scipy.misc.ascent()[:,:,0])
image0.metadata.General.title = 'Rocky Raccoon - R'
axes0 = image0.axes_manager
axes0[0].name = "x"
axes0[1].name = "y"
axes0[0].units = "mm"
axes0[1].units = "mm"

# load lena into 2x3 hyperimage
image1 = hs.signals.Image(np.random.random((2, 3, 512, 512)))
image1.metadata.General.title = 'multi-dimensional Lena'
for i in range(2):
    for j in range(3):
        image1.data[i,j,:] = scipy.misc.ascent()*(i+0.5+j)
axes1 = image1.axes_manager
axes1[2].name = "x"
axes1[3].name = "y"
axes1[2].units = "nm"
axes1[3].units = "nm"

# load green channel of raccoon as an image
image2 = hs.signals.Image(scipy.misc.ascent()[:,:,1])
image2.metadata.General.title = 'Rocky Raccoon - G'
axes2 = image2.axes_manager
axes2[0].name = "x"
axes2[1].name = "y"
axes2[0].units = "mm"
axes2[1].units = "mm"

# load rgb image
rgb = hs.signals.Spectrum(scipy.misc.ascent())
rgb.change_dtype("rgb8")
rgb.metadata.General.title = 'RGB'
axesRGB = rgb.axes_manager
axesRGB[0].name = "x"
axesRGB[1].name = "y"
axesRGB[0].units = "nm"
axesRGB[1].units = "nm"


hs.plot.plot_images([image0, image1, image2, rgb], tight_layout=True,
                       #colorbar='single', 
                       labelwrap=20)


Out[5]:
[<matplotlib.axes._subplots.AxesSubplot at 0x7fabd01dfc50>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabd0086c50>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabcb15e410>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabd07171d0>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabcafd5110>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabcae6d950>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabcad82b50>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabcaca1850>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabcab2f850>]

Real-world use

Another example for this function is plotting EDS line intensities. Using a spectrum image with EDS data, one can use the following commands to get a representative figure of the line intensities. This example also demonstrates changing the colormap (with cmap), adding scalebars to the plots (with scalebar), and changing the padding between the images. The padding is specified as a dictionary, which is used to call matplotlib.figure.Figure.subplots_adjust() (see documentation). Note, this padding can also be changed interactively by clicking on the subplots_adjust button () in the GUI (button may be different when using different graphical backends).

The sample and the data used are described in P. Burdet, et al., Acta Materialia, 61, p. 3090-3098 (2013) (see http://infoscience.epfl.ch/record/185861/).


In [6]:
from urllib import urlretrieve
url = 'http://cook.msm.cam.ac.uk//~hyperspy//EDS_tutorial//'
urlretrieve(url + 'core_shell.hdf5', 'core_shell.hdf5')


Out[6]:
('core_shell.hdf5', <httplib.HTTPMessage instance at 0x7fabd021ce60>)

In [9]:
si_EDS = hs.load("core_shell.hdf5")
im = si_EDS.get_lines_intensity()
hs.plot.plot_images(
    im, tight_layout=True, cmap='RdYlBu_r', axes_decor='off',
    colorbar='single', scalebar='all', 
    scalebar_color='black', suptitle_fontsize=16,
    padding={'top':0.8, 'bottom':0.10, 'left':0.05,
             'right':0.85, 'wspace':0.20, 'hspace':0.10})


/home/fjd29/Anaconda/anaconda2/lib/python2.7/site-packages/matplotlib/figure.py:1653: UserWarning: This figure includes Axes that are not compatible with tight_layout, so its results might be incorrect.
  warnings.warn("This figure includes Axes that are not "
Out[9]:
[<matplotlib.axes._subplots.AxesSubplot at 0x7fabc953df90>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fabcab2f990>]

In [ ]: