-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPlotSpectrum.py
More file actions
58 lines (40 loc) · 1.27 KB
/
PlotSpectrum.py
File metadata and controls
58 lines (40 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import SpectrumFile
import numpy as np
import matplotlib.pyplot as plt
def plot_spectrum(spec, ax=None, **kwargs):
"""
Plot the spectrum, spec (a SpectrumFile object).
If ax is specified, plot it on that axes.
Other **kwargs are passed to plt.semilogy.
"""
if not spec.data.size or not spec.energy.size or np.all(spec.energy == 0):
raise ValueError('Spectrum must be loaded and calibrated')
ax = plot_xy(spec.energy, spec.data, ax=ax, **kwargs)
return ax
def plot_uncal_spectrum(spec, ax=None, **kwargs):
"""
Plot a spectrum vs. channel instead of energy.
spec is a SpectrumFile object.
"""
if not spec.data.size or not spec.channel.size:
raise ValueError('Spectrum must be loaded')
ax = plot_xy(spec.channel, spec.data, ax=ax, xlabel='Channel #', **kwargs)
return ax
def plot_xy(x, y, ax=None, xlabel='Energy [keV]', **kwargs):
"""
Plot x and y as a spectrum.
"""
if not ax:
new_plot = True
plt.figure()
ax = plt.axes()
else:
new_plot = False
plt.semilogy(x, y, axes=ax, drawstyle='steps-mid', **kwargs)
if new_plot:
plt.xlabel(xlabel)
plt.ylabel('Counts')
if 'label' in kwargs:
plt.legend()
plt.show()
return ax