Examples in the Data visualization chapter
In [1]:
%matplotlib qt4
import numpy as np
import hyperspy.api as hs
import matplotlib.pyplot as plt
In [2]:
from urllib.request 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]:
In [3]:
img = hs.load('image*.tif', stack=True)
img.plot(navigator="slider")
In [4]:
s = hs.load('TiFeNi_0*.rpl', stack=True).as_signal1D(0)
s.plot()
In [5]:
im = hs.load('image*.tif', stack=True)
s = hs.load('TiFeNi_0*.rpl', stack=True).as_signal1D(0)
dim = s.axes_manager.navigation_shape
#Rebin the image
im = im.rebin([dim[2], dim[0], dim[1]])
s.plot(navigator=im)
In [6]:
imgSpec = hs.load('TiFeNi_0*.rpl', stack=True)
imgSpec.plot(navigator='spectrum')
In [7]:
imgSpec = hs.load('TiFeNi_0*.rpl', stack=True)
specMax = imgSpec.max(-1).max(-1).max(-1).as_signal1D(0)
imgSpec.plot(navigator=specMax)
In [9]:
from urllib.request import urlretrieve
url = 'http://cook.msm.cam.ac.uk//~hyperspy//EDS_tutorial//'
urlretrieve(url + 'Ni_La_intensity.hdf5', 'Ni_La_intensity.hdf5')
Out[9]:
In [ ]:
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))
In [8]:
import scipy.misc
s = hs.signals.Signal1D(scipy.misc.ascent()[100:160:10])
cascade_plot = hs.plot.plot_spectra(s, style='cascade')
cascade_plot.figure.savefig("cascade_plot.png")
In [9]:
import scipy.misc
s = hs.signals.Signal1D(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[9]:
In [10]:
import scipy.misc
s = hs.signals.Signal1D(scipy.misc.ascent()[100:160:10])
hs.plot.plot_spectra(s, style='heatmap')
Out[10]:
In [12]:
import scipy.misc
s = hs.signals.Signal1D(scipy.misc.ascent()[100:120:10])
hs.plot.plot_spectra(s, style='mosaic')
Out[12]:
In [13]:
import matplotlib.cm
import scipy.misc
s = hs.signals.Signal1D(scipy.misc.ascent()[100:120:10])
ax = hs.plot.plot_spectra(s, style="heatmap")
ax.images[0].set_cmap(matplotlib.cm.jet)
In [14]:
import scipy.misc
s = hs.signals.Signal1D(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.Signal1D(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.Signal1D(scipy.misc.ascent()[100:160:10])
s2 = hs.signals.Signal1D(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()
In [18]:
import scipy.misc
s1 = hs.signals.Signal1D(scipy.misc.face()).as_signal1D(0)[:,:3]
s2 = s1.deepcopy()*-1
hs.plot.plot_signals([s1, s2])
In [19]:
import scipy.misc
s1 = hs.signals.Signal1D(scipy.misc.face()).as_signal1D(0)[:,:3]
s2 = s1.deepcopy()*-1
hs.plot.plot_signals([s1, s2], navigator="slider")
In [20]:
import scipy.misc
s1 = hs.signals.Signal1D(scipy.misc.face()).as_signal1D(0)[:,:3]
s2 = s1.deepcopy()*-1
s3 = hs.signals.Signal1D(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.Signal1D(scipy.misc.face()).as_signal1D(0)[:,:3]
s2 = s1.deepcopy()*-1
hs.plot.plot_signals([s1, s2], sync=False, navigator_list=["slider", "slider"])
In [22]:
s = hs.signals.Signal1D(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)
In [23]:
import scipy
image = hs.signals.Signal2D([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)
Out[23]:
In [24]:
import scipy
image = hs.signals.Signal2D([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)
Out[24]:
In [25]:
import scipy
# load red channel of raccoon as an image
image0 = hs.signals.Signal2D(scipy.misc.face()[:,:,0])
image0.metadata.General.title = 'Rocky Raccoon - R'
# load lena into 6 hyperimage
image1 = hs.signals.Signal2D([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.Signal2D(scipy.misc.face()[:,:,1])
image2.metadata.General.title = 'Rocky Raccoon - G'
# load rgb image of the raccoon
rgb = hs.signals.Signal1D(scipy.misc.face())
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)
Out[25]:
In [26]:
from urllib.request import urlretrieve
url = 'http://cook.msm.cam.ac.uk//~hyperspy//EDS_tutorial//'
urlretrieve(url + 'core_shell.hdf5', 'core_shell.hdf5')
Out[26]:
In [27]:
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[27]:
In [30]:
%%bash
rm core_shell.hdf5 Ni_La_intensity.hdf5 cascade_plot.png
rm Ni_superalloy_010.rpl TiFeNi_010.raw TiFeNi_011.rpl TiFeNi_011.raw image010.tif image011.tif