How to make subplots with image signals?

41 views
Skip to first unread message

Joshua Taillon

unread,
Dec 29, 2014, 6:35:19 PM12/29/14
to hypersp...@googlegroups.com
Pardon if this question has an obvious answer. I'm just recently getting started with hyperspy and still working my way around how to use it most effectively. 

My question is how to most easily generate a figure with a number of subplots. In particular, I am interested in visualizing the results of an ICA output. I have two lists of images that show the line intensities from three elements (EDS maps):

s1 = si_EDS.get_lines_intensity(['Mn_Ka','Zr_La','La_Ma'])
s2 = si_EDS_PCA.get_lines_intensity(['Mn_Ka','Zr_La','La_Ma'])

s1 is the raw EDS signals and s2 is the model created with the ICA results.

I can easily visualize the individual components with 
s1[0].plot()
etc. but this gives me individual figures for each. Is there an easy way to get all the results displayed in a 2x3 subplot array in one figure? This way I would easily be able to compare the differences between the two and export one figure with all the data. I've looked at the code for plot_bss_loadings, as this produces output similar to what I'm looking for, but I couldn't figure out how to apply it to my data.

Thank you in advance, and merry christmas/happy holidays!

- Josh

Francisco

unread,
Dec 30, 2014, 4:03:19 AM12/30/14
to hypersp...@googlegroups.com
In HyperSpy the closest match is the mosaic style of the `plot_spectra` function. However, this produces a n x 1 subplot array and currently there is no way to configure it in order to get a 2 x 3 one. You could modify `plot_spectra` to suit your needs or directly use matplotlib. For example, the following snippet should get you closer to what you want:

    ss = [s1, s2]
    f, axarr = plt.subplots(len(ss), len(s1))
    for n, sr in enumerate(ss):
        for m, s in enumerate(sr):
            axarr[n, m].plot(s.axes_manager[0].axis, s.data)

Happy Christmas!

Francisco

Joshua Taillon

unread,
Dec 30, 2014, 1:32:16 PM12/30/14
to hypersp...@googlegroups.com
Francisco,

Thank you so much for your help! I was struggling with how to get at the data contained in the images themselves, and your short example has made it much more clear. Using your example as a starting point, I hacked together the following solution with matplotlib:

import textwrap
from mpl_toolkits.axes_grid1 import make_axes_locatable


s1 = si_EDS.get_lines_intensity(['Mn_Ka','Zr_La','La_Ma'])
s2 = si_EDS_PCA.get_lines_intensity(['Mn_Ka','Zr_La','La_Ma'])
ss = [s1,s2]

f, axarr = plt.subplots (len(ss), len(s1))
for n, sr in enumerate(ss):
    for m, s in enumerate(sr):
        axes_manager = s.axes_manager
        axes = s.axes_manager.signal_axes
        extent = (axes[0].low_value,
                  axes[0].high_value,
                  axes[1].low_value,
                  axes[1].high_value)
        
        im = axarr[n,m].imshow(s.data, extent=extent, interpolation='nearest')
        
        # Axis labels and tick label settings
        axarr[n,m].set_xlabel(axes[0].units,fontsize=8)
        axarr[n,m].set_ylabel(axes[1].units,fontsize=8)
        axarr[n,m].tick_params(axis='both', which='major', labelsize=8)
        
        # Title information
        title = s.metadata.General.title
        wrappedTitle = textwrap.fill(title, 25)
        axarr[n,m].set_title('%s' % (wrappedTitle),fontsize=10)
        
        # Color bar settings
        divider = make_axes_locatable(axarr[n,m])
        cax = divider.append_axes("right", size="5%", pad=0.05)
        cbar = plt.colorbar(im, cax = cax)
        cbar.ax.tick_params(labelsize=8)

f.suptitle('EDS maps of Mn, Zr, and La before and after PCA and ICA', fontsize=12)
f.tight_layout()

left  = 0.125  # the left side of the subplots of the figure
right = 0.9    # the right side of the subplots of the figure
bottom = 0.1   # the bottom of the subplots of the figure
top = 0.92      # the top of the subplots of the figure
wspace = 0.2   # the amount of width reserved for blank space between subplots
hspace = 0.01   # the amount of height reserved for white space between subplots
subplots_adjust(left=None, bottom=None, right=None, top=top, wspace=None, hspace=hspace)
plt.close('all')
f

Perhaps a useful function could be a `plot_images` function that behaves similarly to plot_spectra, but would work for images. As it is, I was not able to use the plot_spectra with the 2D images, but this could be more due to my unfamiliarity with the code than anything else.

Thanks,
Josh
Reply all
Reply to author
Forward
0 new messages