Data visualization Doc

Examples in the Data visualization chapter


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

Customizing the “navigator”


In [2]:
from urllib import urlretrieve
url = 'http://cook.msm.cam.ac.uk//~hyperspy//EDS_tutorial//'
urlretrieve(url + 'TiFeNi_010.rpl', 'Ni_superalloy_010.rpl')
urlretrieve(url + 'TiFeNi_010.raw', 'TiFeNi_010.raw')
urlretrieve(url + 'TiFeNi_011.rpl', 'TiFeNi_011.rpl')
urlretrieve(url + 'TiFeNi_011.raw', 'TiFeNi_011.raw')
urlretrieve(url + 'image010.tif', 'image010.tif')
urlretrieve(url + 'image011.tif', 'image011.tif')


Out[2]:
('image011.tif', <httplib.HTTPMessage instance at 0x7fb0a6d277a0>)

In [3]:
img = hs.load('image*.tif', stack=True)
img.plot(navigator="slider")


Loading individual files

Individual files loaded correctly

	Title: doc_docstr_examples
	Signal type: 
	Data dimensions: (2, 1024, 768)
	Data representation: image
	Data type: uint8
/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'):

In [4]:
s = hs.load('TiFeNi_0*.rpl', stack=True).as_spectrum(0)
s.plot()


Loading as Image

Individual files loaded correctly

	Title: doc_docstr_examples
	Signal type: 
	Data dimensions: (1024, 1, 128, 96)
	Data representation: image
	Data type: int16

In [5]:
im = hs.load('image*.tif', stack=True)
s = hs.load('TiFeNi_0*.rpl', stack=True).as_spectrum(0)
dim = s.axes_manager.navigation_shape
#Rebin the image
im = im.rebin([dim[2], dim[0], dim[1]])
s.plot(navigator=im)


Loading individual files

Individual files loaded correctly

	Title: doc_docstr_examples
	Signal type: 
	Data dimensions: (2, 1024, 768)
	Data representation: image
	Data type: uint8
Loading as Image

Individual files loaded correctly

	Title: doc_docstr_examples
	Signal type: 
	Data dimensions: (1024, 1, 128, 96)
	Data representation: image
	Data type: int16

In [6]:
imgSpec = hs.load('TiFeNi_0*.rpl', stack=True)
imgSpec.plot(navigator='spectrum')


Loading as Image

Individual files loaded correctly

	Title: doc_docstr_examples
	Signal type: 
	Data dimensions: (1024, 1, 128, 96)
	Data representation: image
	Data type: int16

In [7]:
imgSpec = hs.load('TiFeNi_0*.rpl', stack=True)
specMax = imgSpec.max(-1).max(-1).max(-1).as_spectrum(0)
imgSpec.plot(navigator=specMax)


Loading as Image

Individual files loaded correctly

	Title: doc_docstr_examples
	Signal type: 
	Data dimensions: (1024, 1, 128, 96)
	Data representation: image
	Data type: int16

Using Mayavi to visualize 3D data


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


Out[8]:
('Ni_La_intensity.hdf5', <httplib.HTTPMessage instance at 0x7fb0a15f2b90>)

In [9]:
from mayavi import mlab
ni = hs.load('Ni_La_intensity.hdf5')
mlab.figure()
mlab.contour3d(ni.data, contours=[85])
mlab.outline(color=(0, 0, 0))


WARNING:traits.has_traits:DEPRECATED: traits.has_traits.wrapped_class, 'the 'implements' class advisor has been deprecated. Use the 'provides' class decorator.
Out[9]:
<mayavi.modules.outline.Outline at 0x7fb08f31d770>

plot_spectra


In [10]:
import scipy.misc
s = hs.signals.Spectrum(scipy.misc.ascent()[100:160:10])
cascade_plot = hs.plot.plot_spectra(s, style='cascade')
cascade_plot.figure.savefig("cascade_plot.png")

In [11]:
import scipy.misc
s = hs.signals.Spectrum(scipy.misc.ascent()[100:160:10])
color_list = ['red', 'red', 'blue', 'blue', 'red', 'red']
line_style_list = ['-','--','steps','-.',':','-']
hs.plot.plot_spectra(s, style='cascade', color=color_list,
line_style=line_style_list,legend='auto')


Out[11]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fb08f212490>

In [12]:
import scipy.misc
s = hs.signals.Spectrum(scipy.misc.ascent()[100:160:10])
hs.plot.plot_spectra(s, style='heatmap')


Out[12]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fb08edbb9d0>

In [13]:
import scipy.misc
s = hs.signals.Spectrum(scipy.misc.ascent()[100:120:10])
hs.plot.plot_spectra(s, style='mosaic')


Out[13]:
array([<matplotlib.axes._subplots.AxesSubplot object at 0x7fb08ec0bf50>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x7fb08eb9f050>], dtype=object)

In [14]:
import matplotlib.cm
import scipy.misc
s = hs.signals.Spectrum(scipy.misc.ascent()[100:120:10])
ax = hs.plot.plot_spectra(s, style="heatmap")
ax.images[0].set_cmap(matplotlib.cm.jet)

In [15]:
import scipy.misc
s = hs.signals.Spectrum(scipy.misc.ascent()[100:160:10])
legendtext = ['Plot 0', 'Plot 1', 'Plot 2', 'Plot 3', 'Plot 4', 'Plot 5']
cascade_plot = hs.plot.plot_spectra(
s, style='cascade', legend=legendtext, dpi=60,
facecolor='lightblue', frameon=True, num=5)
cascade_plot.set_xlabel("X-axis")
cascade_plot.set_ylabel("Y-axis")
cascade_plot.set_title("Cascade plot")
plt.draw()

In [16]:
import scipy.misc
s = hs.signals.Spectrum(scipy.misc.ascent()[100:160:10])
cascade_plot = hs.plot.plot_spectra(s)
cascade_plot.set_xlabel("An axis")
cascade_plot.set_ylabel("Another axis")
cascade_plot.set_title("A title!")
plt.draw()

In [17]:
import scipy.misc
fig, axarr = plt.subplots(1,2)
s1 = hs.signals.Spectrum(scipy.misc.ascent()[100:160:10])
s2 = hs.signals.Spectrum(scipy.misc.ascent()[200:260:10])
hs.plot.plot_spectra(s1, style='cascade',color='blue',ax=axarr[0],fig=fig)
hs.plot.plot_spectra(s2, style='cascade',color='red',ax=axarr[1],fig=fig)
fig.canvas.draw()

Plot_signals


In [18]:
import scipy.misc
s1 = hs.signals.Spectrum(scipy.misc.ascent()).as_spectrum(0)[:,:3]
s2 = s1.deepcopy()*-1
hs.plot.plot_signals([s1, s2])

In [19]:
import scipy.misc
s1 = hs.signals.Spectrum(scipy.misc.ascent()).as_spectrum(0)[:,:3]
s2 = s1.deepcopy()*-1
hs.plot.plot_signals([s1, s2], navigator="slider")

In [20]:
import scipy.misc
s1 = hs.signals.Spectrum(scipy.misc.ascent()).as_spectrum(0)[:,:3]
s2 = s1.deepcopy()*-1
s3 = hs.signals.Spectrum(np.linspace(0,9,9).reshape([3,3]))
hs.plot.plot_signals([s1, s2], navigator_list=["slider", s3])

In [21]:
import scipy.misc
s1 = hs.signals.Spectrum(scipy.misc.ascent()).as_spectrum(0)[:,:3]
s2 = s1.deepcopy()*-1
hs.plot.plot_signals([s1, s2], sync=False, navigator_list=["slider", "slider"])

In [25]:
s = hs.signals.Spectrum(np.arange(100).reshape([10,10]))
s.plot(navigator='spectrum')
for i in range(s.axes_manager.shape[0]):
    m = hs.plot.markers.text(y=s.sum(-1).data[i]+5,
                             x=i, text='abcdefghij'[i])
s.add_marker(m, plot_on_signal=False)
x = s.axes_manager.shape[-1]/2 #middle of signal plot
m = hs.plot.markers.text(x=x, y=s[:, x].data+2,text=[i for i in 'abcdefghij'])
s.add_marker(m)

Plot_images


In [26]:
import scipy
image = hs.signals.Image([scipy.misc.ascent()]*6)
angles = hs.signals.Signal(range(10,70,10))
angles.axes_manager.set_signal_dimension(0)
image.map(scipy.ndimage.rotate, angle=angles, reshape=False)
hs.plot.plot_images(image, tight_layout=True)


calculating 100% |#############################################| ETA:  00:00:00 

/home/fjd29/Python/hyperspy/hyperspy/drawing/utils.py:753: UserWarning: Axes labels were requested, but one or both of the axes units and/or name are undefined. Axes decorations have been set to 'ticks' instead.
  'Axes labels were requested, but one '
Out[26]:
[<matplotlib.axes._subplots.AxesSubplot at 0x7fb0a18af3d0>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08d6a3cd0>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08dee4910>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08e137390>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08d658c50>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08d42a490>]

In [27]:
import scipy
image = hs.signals.Image([scipy.misc.ascent()]*6)
angles = hs.signals.Signal(range(10,70,10))
angles.axes_manager.set_signal_dimension(0)
image.map(scipy.ndimage.rotate, angle=angles, reshape=False)
hs.plot.plot_images(
image, suptitle='Turning Lena', axes_decor='off',
label=['Rotation ' + str(angle.data[0]) +
'$^\degree$' for angle in angles], colorbar=None)


calculating 100% |#############################################| ETA:  00:00:00 

Out[27]:
[<matplotlib.axes._subplots.AxesSubplot at 0x7fb08d74d710>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08d4dd110>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08da24650>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08d4d35d0>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08d1ecc50>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08d660110>]

In [29]:
import scipy


# load red channel of raccoon as an image
image0 = hs.signals.Image(scipy.misc.ascent()[:,:,0])
image0.metadata.General.title = 'Rocky Raccoon - R'


# load lena into 6 hyperimage
image1 = hs.signals.Image([scipy.misc.ascent()]*6)
angles = hs.signals.Signal(range(10,70,10))
angles.axes_manager.set_signal_dimension(0)
image1.map(scipy.ndimage.rotate, angle=angles, reshape=False)


# load green channel of raccoon as an image
image2 = hs.signals.Image(scipy.misc.ascent()[:,:,1])
image2.metadata.General.title = 'Rocky Raccoon - G'


# load rgb image of the raccoon
rgb = hs.signals.Spectrum(scipy.misc.ascent())
rgb.change_dtype("rgb8")
rgb.metadata.General.title = 'Raccoon - RGB'
    

images = [image0, image1, image2, rgb]
for im in images:
    ax = im.axes_manager.signal_axes
    ax[0].name, ax[1].name = 'x', 'y'
    ax[0].units, ax[1].units = 'mm', 'mm'
hs.plot.plot_images(images, tight_layout=True,
colorbar='single', labelwrap=20)


calculating 100% |#############################################| ETA:  00:00:00 

/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[29]:
[<matplotlib.axes._subplots.AxesSubplot at 0x7fb08d1d98d0>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08d22b6d0>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08d2bc710>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08ce32350>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08cda4d90>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08cba8b90>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08cb1ee10>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08cab7dd0>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08ca2bed0>]

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


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

In [31]:
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', saturated_pixels=0.0, 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})


Out[31]:
[<matplotlib.axes._subplots.AxesSubplot at 0x7fb08c49bc50>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fb08c4f0310>]

In [ ]: