We will introduce various matplotlib wrappers in skspec
. For interactive plotting, see our plotly tutorial. For explanation of test_data, see related pre-packaged datasets. 2d and 3d skspec plotting is described in a separate tutorial.
Configure notebook style (see NBCONFIG.ipynb), add imports and paths. The %run magic used below requires IPython 2.0 or higher.
%run NBCONFIG.ipynb
Populating the interactive namespace from numpy and matplotlib
TimeSpectra.plot()
is merely a wrapper to the pandas dataframe.plot() method. It has some bells and whistles like colorbars, and default labels/legend settings.
from skspec.data import aunps_glass
ts = aunps_glass(style=1)
ts.plot(cbar=True);
WARNING:skspec.core.spectra:Spectrum does not have subtracted baseline; could affect result in specious absorbance data.
ts.plot()
will reflect changes in the spectral, temporal and intensity units of the plot. Let's now express the data as a time interval, and change the spectral unit. We'll also look at the normalized data (relative to the first curve) by setting ts.iunit = 'r'
t2 = ts.as_varunit('m')
t2 = t2.as_iunit('r')
t2.name = 'Normalized nanoparticle data.'
t2.plot(cbar=True, cmap='hot');
WARNING:skspec.core.spectra:Spectrum does not have subtracted baseline; could affect result in specious absorbance data.
Likewise, we can compute the area directly from ts and plot it
area = ts.area() #area is a hybrid array
plt.plot(area.transpose());
Because areaplots and referenced data (absorbance, transmittance etc...) are so important, skspec includes a few simple wrapper plots to reduce the boilerplate. For example, instead of bothering with ts.area().transpose()
, we can just use areaplot(ts)
. This will give a more clear y-axis label, and title by default:
from skspec.plotting import areaplot
areaplot(ts);
WARNING:skspec.plotting.basic_plots:Recomputing area from shape (704, 100) to (1, 100) WARNING:skspec.plotting.basic_plots:areaplot() forcing transpose on AuNPs Glass (good) for plotting
skspec
provides a shortcut keyword to plot data as Absorbance, Transmittance etc... see the primary tutorial for an explanation of these various representations.
For now, let's plot the data in the iunit='r' representation, which simply is dividing all the curves in the set by the reference curve: in this case, that's the first spectrum (t=0).
ts.plot(ts, iunit='r');
WARNING:skspec.core.spectra:Spectrum does not have subtracted baseline; could affect result in specious absorbance data.
Another very common operation is slicing the data into subsets of wavelengths. For example, one may ask how the curve varies between ${\bf400nm \lt \lambda \le 450nm}$. To simplify this, there is the TimeSpectra method ts.wavelength_slices()
and the range_timeplot
(or stripchart as its sometimes called).
from skspec.plotting import range_timeplot
ts_by_8 = ts.wavelength_slices(8) # Eight even slices
range_timeplot(ts_by_8);
skspec has builtin multiplot support, but is designed to be flexible in creating arbitrary multiplots. One such builtin plot is the quadplot()
, which essentially summarized the plots seen so far:
from skspec.plotting import quad_plot
fig = quad_plot(ts, striplegend=True) #Show strip chart wavelength legend:
#help(quad_plot) for other options
WARNING:skspec.core.spectra:Spectrum does not have subtracted baseline; could affect result in specious absorbance data. WARNING:skspec.core.spectra:Spectrum does not have subtracted baseline; could affect result in specious absorbance data. WARNING:skspec.plotting.basic_plots:Recomputing area from shape (704, 100) to (1, 100) WARNING:skspec.plotting.basic_plots:areaplot() forcing transpose on AuNPs Glass (good) for plotting
Let's split the data into 5 sets and show this as a multiplot. We can easily split the data using ts.splitby(n, axis=1)
. We'll also normalize the data using as_iunit('r')
Sampling will automatically store subsets in a skspec.SpecStack object; see the SpecStack tutorial for more help.
stack = ts.split_by(5)
stack.iunit = 'r'
stack.reference = 0 # FLIP THESE FOR BUG
stack.plot(figsize=(8,8), colormap='RdBu');
WARNING:skspec.core.utilities:Returning uneven sampling for (n=5) along axis 1of size 100 WARNING:skspec.core.specstack:Generating keys spec_0-spec_4 WARNING:skspec.core.spectra:Reference changed based on column data whileSpectra was normalized. WARNING:skspec.core.spectra:Reference changed based on column data whileSpectra was normalized. WARNING:skspec.core.spectra:Reference changed based on column data whileSpectra was normalized. WARNING:skspec.core.spectra:Reference changed based on column data whileSpectra was normalized. WARNING:skspec.core.spectra:Reference changed based on column data whileSpectra was normalized. WARNING:skspec.core.spectra:Spectrum does not have subtracted baseline; could affect result in specious absorbance data. WARNING:skspec.core.spectra:Spectrum does not have subtracted baseline; could affect result in specious absorbance data. WARNING:skspec.core.spectra:Spectrum does not have subtracted baseline; could affect result in specious absorbance data. WARNING:skspec.core.spectra:Spectrum does not have subtracted baseline; could affect result in specious absorbance data. WARNING:skspec.core.spectra:Spectrum does not have subtracted baseline; could affect result in specious absorbance data.
from skspec.core.specindex import SpecIndex
%pylab inline
SpecIndex(np.linspace(50,50.5))
Populating the interactive namespace from numpy and matplotlib
SpecIndex[None]([50.0, 50.0102040816, 50.0204081633, 50.0306122449, 50.0408163265, 50.0510204082, 50.0612244898, 50.0714285714, 50.0816326531, 50.0918367347, 50.1020408163, 50.112244898, 50.1224489796, 50.1326530612, 50.1428571429, 50.1530612245, 50.1632653061, 50.1734693878, 50.1836734694, 50.193877551, 50.2040816327, 50.2142857143, 50.2244897959, 50.2346938776, 50.2448979592, 50.2551020408, 50.2653061224, 50.2755102041, 50.2857142857, 50.2959183673, 50.306122449, 50.3163265306, 50.3265306122, 50.3367346939, 50.3469387755, 50.3571428571, 50.3673469388, 50.3775510204, 50.387755102, 50.3979591837, 50.4081632653, 50.4183673469, 50.4285714286, 50.4387755102, 50.4489795918, 50.4591836735, 50.4693877551, 50.4795918367, 50.4897959184, 50.5], dtype='float64')
Notice that 5 subplots were created for us automatically. You can specify the number of columns to SpecStack.plot()
through the ncols
keyword (default =3). e.g. stack.plot(ncols=2)
Custom multiplots are easy because skspec and pandas plots take references to any maplotlib Axes object. You can generate empty Axes via plt.subplots()
or through the skspec subplots wrapper splot
. splot is almost identicaly to plt.subplots(), but does not require you to nest your axes calls. For example, a (2,3) subplot could be generated in the following ways:
fig, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(2,3)
ax1, ax2, ax3, ax4, ax5, ax6 = splot(2,3)
I often find this nesting superfulous for my tasks. splot
has some other features (help(splot)) not described here.
Let's use splot to compare wavelengths regions of the spectra
from skspec.plotting import splot
ax1, ax2 = splot(1,2)
# Wavelength slices (up to 500, 600 on)
ts[:500.0].plot(ax=ax1, title='Short Wavelengths', color='b')
ts[600.0:].plot(ax=ax2, title='Long Wavelengths', color='r')
ax2.yaxis.set_label_position('right')
WARNING:skspec.core.spectra:Spectrum does not have subtracted baseline; could affect result in specious absorbance data. WARNING:skspec.core.spectra:Spectrum does not have subtracted baseline; could affect result in specious absorbance data.
As one last example, let's show off this data set, normalized by the first curve, in four representations: transmittance, absorbance (base 10), absorbance (base e), and percent transmittance.
ax1, ax2, ax3, ax4 = splot(2,2, figsize=(8,8))
ts.plot(ax=ax1, iunit='t')
ts.plot(ax=ax2, iunit='a')
ts.plot(ax=ax3, iunit='a(ln)', title='')
ts.plot(ax=ax4, iunit='%t', title='');
ax2.yaxis.set_label_position('right')
ax4.yaxis.set_label_position('right')
WARNING:skspec.core.spectra:Spectrum does not have subtracted baseline; could affect result in specious absorbance data. WARNING:skspec.core.spectra:Spectrum does not have subtracted baseline; could affect result in specious absorbance data. WARNING:skspec.core.spectra:Spectrum does not have subtracted baseline; could affect result in specious absorbance data. WARNING:skspec.core.spectra:Spectrum does not have subtracted baseline; could affect result in specious absorbance data.