Data visualization Doc

Examples in the Data visualization chapter


In [12]:
%matplotlib qt
#%matplotlib inline

Customizing the “navigator”


In [13]:
>>> 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[13]:
('image011.tif', <httplib.HTTPMessage instance at 0x000000001B236408>)

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


Loading individual files

Individual files loaded correctly

	Title: doc_docstr
	Signal type: 
	Data dimensions: (2, 1024, 768)
	Data representation: image
	Data type: uint8

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


Loading as Image

Individual files loaded correctly

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

In [16]:
>>> im = load('image*.tif', stack=True)
    >>> s = 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
	Signal type: 
	Data dimensions: (2, 1024, 768)
	Data representation: image
	Data type: uint8
Loading as Image

Individual files loaded correctly

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

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


Loading as Image

Individual files loaded correctly

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

In [18]:
>>> imgSpec = 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
	Signal type: 
	Data dimensions: (1024, 1, 128, 96)
	Data representation: image
	Data type: int16

Using Mayavi to visualize 3D data


In [19]:
>>> 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[19]:
('Ni_La_intensity.hdf5', <httplib.HTTPMessage instance at 0x000000001BD34C88>)

In [20]:
>>> from mayavi import mlab
    >>> ni = 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.
C:\Anaconda\lib\site-packages\traits\has_traits.py:1766: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  setattr( self, name, value )
Out[20]:
<mayavi.modules.outline.Outline at 0x2d3f9b48>

plot_spectra


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

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


Out[22]:
<matplotlib.axes._subplots.AxesSubplot at 0x2db94a58>

In [23]:
>>> import scipy.misc
    >>> s = signals.Spectrum(scipy.misc.lena()[100:160:10])
    >>> utils.plot.plot_spectra(s, style='heatmap')


Out[23]:
<matplotlib.axes._subplots.AxesSubplot at 0x2dc74550>

In [24]:
>>> import scipy.misc
    >>> s = signals.Spectrum(scipy.misc.lena()[100:120:10])
    >>> utils.plot.plot_spectra(s, style='mosaic')


Out[24]:
array([<matplotlib.axes._subplots.AxesSubplot object at 0x000000002DFC8BA8>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x000000002E154400>], dtype=object)

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

In [26]:
>>> import scipy.misc
    >>> s = signals.Spectrum(scipy.misc.lena()[100:160:10])
    >>> legendtext = ['Plot 0', 'Plot 1', 'Plot 2', 'Plot 3', 'Plot 4', 'Plot 5']
    >>> cascade_plot = utils.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 [27]:
>>> import scipy.misc
    >>> s = signals.Spectrum(scipy.misc.lena()[100:160:10])
    >>> cascade_plot = utils.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 [28]:
>>> import scipy.misc
    >>> fig, axarr = plt.subplots(1,2)
    >>> s1 = signals.Spectrum(scipy.misc.lena()[100:160:10])
    >>> s2 = signals.Spectrum(scipy.misc.lena()[200:260:10])
    >>> utils.plot.plot_spectra(s1, style='cascade',color='blue',ax=axarr[0],fig=fig)
    >>> utils.plot.plot_spectra(s2, style='cascade',color='red',ax=axarr[1],fig=fig)
    >>> fig.canvas.draw()

Plot_signals


In [29]:
>>> import scipy.misc
    >>> s1 = signals.Spectrum(scipy.misc.face()).as_spectrum(0)[:,:3]
    >>> s2 = s1.deepcopy()*-1
    >>> utils.plot.plot_signals([s1, s2])

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


C:\Anaconda\lib\site-packages\matplotlib\pyplot.py:423: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_num_figures`).
  max_open_warning, RuntimeWarning)

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

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

In [33]:
>>> s = signals.Spectrum(np.arange(100).reshape([10,10]))
    >>> s.plot(navigator='spectrum')
    >>> for i in range(s.axes_manager.shape[0]):
    >>>     m = utils.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 = utils.plot.markers.text(x=x, y=s[:, x].data+2,
    >>>                             text=[i for i in 'abcdefghij'])
    >>> s.add_marker(m)

Plot_images


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


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

c:\users\pb565\documents\python\hyperspy\hyperspy\drawing\utils.py:754: 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[34]:
[<matplotlib.axes._subplots.AxesSubplot at 0x36c57a20>,
 <matplotlib.axes._subplots.AxesSubplot at 0x38782668>,
 <matplotlib.axes._subplots.AxesSubplot at 0x388988d0>,
 <matplotlib.axes._subplots.AxesSubplot at 0x38af5080>,
 <matplotlib.axes._subplots.AxesSubplot at 0x38d12438>,
 <matplotlib.axes._subplots.AxesSubplot at 0x38eec278>]

In [35]:
>>> import scipy
    >>> image = signals.Image([scipy.misc.lena()]*6)
    >>> angles = signals.Signal(range(10,70,10))
    >>> angles.axes_manager.set_signal_dimension(0)
    >>> image.map(scipy.ndimage.rotate, angle=angles, reshape=False)
    >>> utils.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[35]:
[<matplotlib.axes._subplots.AxesSubplot at 0x3959d6d8>,
 <matplotlib.axes._subplots.AxesSubplot at 0x39602cf8>,
 <matplotlib.axes._subplots.AxesSubplot at 0x39d181d0>,
 <matplotlib.axes._subplots.AxesSubplot at 0x396cbcf8>,
 <matplotlib.axes._subplots.AxesSubplot at 0x39de0ef0>,
 <matplotlib.axes._subplots.AxesSubplot at 0x39e60748>]

In [36]:
>>> import scipy

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

    >>> # load lena into 6 hyperimage
    >>> image1 = signals.Image([scipy.misc.lena()]*6)
    >>> angles = 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 = signals.Image(scipy.misc.face()[:,:,1])
    >>> image2.metadata.General.title = 'Rocky Raccoon - G'

    >>> # load rgb image of the raccoon
    >>> rgb = signals.Spectrum(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'
    >>> utils.plot.plot_images(images, tight_layout=True, 
    >>>                        colorbar='single', labelwrap=20)


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

C:\Anaconda\lib\site-packages\matplotlib\figure.py:1644: 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[36]:
[<matplotlib.axes._subplots.AxesSubplot at 0x39e52fd0>,
 <matplotlib.axes._subplots.AxesSubplot at 0x3a402160>,
 <matplotlib.axes._subplots.AxesSubplot at 0x3a39b2b0>,
 <matplotlib.axes._subplots.AxesSubplot at 0x3a27f8d0>,
 <matplotlib.axes._subplots.AxesSubplot at 0x3a29c5c0>,
 <matplotlib.axes._subplots.AxesSubplot at 0x3a539898>,
 <matplotlib.axes._subplots.AxesSubplot at 0x3a613390>,
 <matplotlib.axes._subplots.AxesSubplot at 0x3a6d4400>,
 <matplotlib.axes._subplots.AxesSubplot at 0x3a7e94e0>]

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


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

In [41]:
>>> si_EDS = load("core_shell.hdf5")
    >>> im = si_EDS.get_lines_intensity()
    >>> utils.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[41]:
[<matplotlib.axes._subplots.AxesSubplot at 0x43ba9e80>,
 <matplotlib.axes._subplots.AxesSubplot at 0x43ca2908>]

In [ ]: