<jupyter_start><jupyter_text>Various examples of Plotting a Spectrum<jupyter_code>import becquerel as bq
import numpy as np
import matplotlib.pyplot as plt

spec1 = bq.Spectrum.from_file('../tests/samples/1110C NAA cave pottery.Spe')
spec2 = bq.Spectrum.from_file('../tests/samples/Mendocino_07-10-13_Acq-10-10-13.Spe')<jupyter_output>SpeFile: Reading file ../tests/samples/1110C NAA cave pottery.Spe
SpeFile: Reading file ../tests/samples/Mendocino_07-10-13_Acq-10-10-13.Spe<jupyter_text>Spectrum plot methodA spectrum has a plot method now. By default it plots a spectrum on a linear scale and adds the filename of the spectrum if available as a title. The x axes is in keV if calibrated, otherwise in channels. The y axis is in counts or cps.<jupyter_code>spec1.plot()
plt.show()<jupyter_output><empty_output><jupyter_text>It can take a xmode and ymode argument to define what will be plotted on the respective axis. It also takes a yscale ('linear', 'log', 'logit', 'symlog') and a title argument. Furthermore, limits can be passed to the plot function; if set to 'default' it will do some internal scaling and overwrite the matplotlib methods. The x and y labels can be overwritten too.<jupyter_code>spec1.plot('-r', xmode='channel', ymode='cps', yscale='symlog', xlim=(1500,2000), 
           ylim='default', title='Alternative scaling', xlabel="Channels [a.u.]")
plt.show()<jupyter_output><empty_output><jupyter_text>A spectrum also has a fill_between method and can take an axes object. If multiple spectrum are plotted, the last call will overwrite the arguments of previous calls (title, yscale). Any matplotlib's plot and fill_between keyword arguments can be passed to the plotting functions.<jupyter_code>fig, ax = plt.subplots()
ax.set_xlim((1000, 1500))
spec2.plot('--', xmode='keV', ymode='cnt', ax=ax, label="Spectrum 1", color='red')
spec1.fill_between(xmode='energy', ymode='counts', ax=ax, yscale='log', title='Two spectrums in one plot',
                   label="Spectrum 2", edgecolor='black', facecolor=(0.1,0.5,0.1,0.5))
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>The plotting command returns an axis object, which can be used to change plot properties.<jupyter_code>ax = spec1.plot(xmode='energy', ymode='cpskev', yscale='log', title='Spectrum with custom label')
ax.set_xlabel("Custom energies [MeV]")
ax.set_xlim((500,2000))
plt.show()<jupyter_output><empty_output><jupyter_text>The SpectrumPlotter classThere is also a SpectrumPlotter which is quite powerful and can be used for producing custom matplotlib plots of Spectrums. There are additional methods available such as get_corners() and properties such as xlim, ylim, linthresh. If the matplotlib default values are not good enough these might be the ones you want.<jupyter_code>from becquerel import SpectrumPlotter
plt.figure()
sp = SpectrumPlotter(spec1, xmode='energy', ymode='cpskev', yscale='symlog')
xcorners, ycorners = sp.get_corners()
plt.plot(xcorners, ycorners)
plt.xlabel(sp.xlabel)
plt.ylabel(sp.ylabel)
plt.yscale('symlog', linthresh=sp.linthresh)
plt.xlim(sp.xlim)
plt.ylim(sp.ylim)
plt.show()<jupyter_output><empty_output><jupyter_text>The SpectrumPlotter class has it's own plot and fill_between method. They also take matplotlib function keyword arguments, which overwrite previously defined properties. This is what the Spectrum plot and fill_between call do.<jupyter_code>SpectrumPlotter(spec2, yscale='log', color="red").plot('.', color="black")
plt.show()<jupyter_output><empty_output><jupyter_text>The plot can also be configured by using the SpectrumPlotters setter methods. This is how the class is implemented internally.<jupyter_code>fig, ax = plt.subplots()
sp = SpectrumPlotter(spec1)
sp.ax=ax
sp.xlabel="My x Label"
sp.ylabel="My y Label"
sp.xlim=(1000,1400)
sp.yscale='log'
sp.ylim='default'
sp.xmode='energy'
sp.ymode='cps'
sp.title='My custom title'
sp.fill_between(edgecolor='black', facecolor=(0.1,0.5,0.1,0.5))
plt.show()<jupyter_output><empty_output><jupyter_text>Errorbars and errorbandsIt is also possible to create error bands and errorbars with the plot method:<jupyter_code>spec1.plot(xmode='channel', ymode='counts', emode='band', xlim=(3000,3100), ylim=(0,100), label='Spectrum')
plt.legend(loc=2)
spec1.plot(xmode='channel', ymode='counts', emode='bars', xlim=(3000,3030), ylim=(0,50), label='Spectrum')
plt.legend(loc=3)
plt.show()<jupyter_output><empty_output><jupyter_text>The control of the errorbars and bands is quite limited with the Spectrum.plot function (it also suppresses a legend entry as you can see above); for more advanced uses, use the SpectrumPlotter class and its errorband and errorbar function<jupyter_code>sp = SpectrumPlotter(spec1, xmode='channel', ymode='counts', xlim=(3000,3100), ylim=(0,100))
sp.plot(color='black', label='Spectrum')
sp.errorband(color='green', label='Error band')
plt.legend(loc=2)


sp = SpectrumPlotter(spec1, xmode='channel', ymode='counts', xlim=(3000,3030), ylim=(0,50))
sp.plot(color='black', label='Spectrum')
sp.errorbar(color='green', capsize=4, fmt='x', label='Error bars')
plt.legend(loc=3)

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12772161741835147, 'fraction_numerical': 0.028382581648522552, 'mean_word_length': 6.713643178410795, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '147021', 'license': 'BSD-3-Clause-LBNL', 'n_tokens_mistral': 1859, 'n_tokens_neox': 1699, 'n_words': 494, 'path': 'examples/plotting.ipynb', 'repo_name': 'dhellfeld/becquerel', 'size': '5144'}