-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPlotFuncs.py
218 lines (192 loc) · 9.24 KB
/
PlotFuncs.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import matplotlib
import platform
import matplotlib.pyplot as plt
import numpy as np
import math
# Use appropriate backend if on MacOS
if platform.system() == 'Darwin':
matplotlib.use('MacOSX')
# Set matplotlib style
plt.style.use('./allcrs.mplstyle')
# Utility function to save figure
def MySaveFig(fig, pltname, pngsave=False):
if pngsave:
fig.savefig(f"{pltname}.png", bbox_inches='tight', dpi=300)
print(f"Saving plot as {pltname}.png")
fig.savefig(f"{pltname}.pdf", bbox_inches='tight', dpi=300)
print(f"Saving plot as {pltname}.pdf")
class TheCrSpectrum:
"""Class for plotting cosmic ray spectrum data."""
# Experiment colors as a dictionary to avoid multiple attributes
colors = {
'AMS-02': 'forestgreen',
'AUGER': 'steelblue',
'BESS': 'yellowgreen',
'CALET': 'darkcyan',
'CREAM': 'r',
'DAMPE': 'm',
'FERMI': 'b',
'HAWC': 'slategray',
'HESS': 'darkorchid',
'IceCube': 'salmon',
'ICETOP_ICECUBE': 'c',
'KASCADE': 'darkgoldenrod',
'KASCADE-Grande': 'goldenrod',
'NUCLEON': 'sienna',
'PAMELA': 'darkorange',
'TA': 'crimson',
'TIBET': 'indianred',
'TUNKA-133': 'hotpink',
'VERITAS': 'seagreen'
}
colors = dict(reversed(list(colors.items())))
def __init__(self):
print("Initializing TheCrSpectrum")
def FigSetup(self, shape='Rectangular'):
"""Sets up the figure based on the shape."""
figsize = (16.5, 5) if shape == 'Wide' else (10.0, 10.5)
fig, ax = plt.subplots(figsize=figsize)
self.SetAxes(ax)
return fig, ax
def SetAxes(self, ax):
"""Configures the x and y axes for the plot."""
ax.minorticks_off()
ax.set_xscale('log')
ax.set_xlim([.1, 1e12])
ax.set_xticks([1e0, 1e3, 1e6, 1e9, 1e12])
ax.set_xlabel('Energy [GeV]')
ax.set_yscale('log')
ax.set_ylim([1e-7, 1e4])
ax.set_ylabel(r'E$^{2}$ Intensity [GeV m$^{-2}$ s$^{-1}$ sr$^{-1}$]')
# Twin axis for Joules
ax2 = ax.twiny()
ax2.minorticks_off()
ax2.set_xscale('log')
ax2.set_xlabel('Energy [J]', color='tab:blue', labelpad=18)
eV2Joule = 1.60218e-19
ax2.set_xlim([.1 * 1e9 * eV2Joule, 1e21 * eV2Joule])
ax2.set_xticks([1e-10, 1e-8, 1e-6, 1e-4, 1e-2, 1e0, 1e2])
ax2.tick_params(axis='x', colors='tab:blue')
def annotate(self, ax):
"""Annotates specific points on the plot."""
s_LHC = 14e3**2.0 # GeV2
proton_mass = 0.938 # GeV
E_LHC = 0.5 * s_LHC / proton_mass
annotations = [
('LHC', E_LHC, 1e-7, E_LHC, 1e-6),
('Knee', 2.8e6, 1., 2.5e5, 1.3e-1),
('Ankle', 0.7e10, 4e-4, 1.25e10, 3e-3),
]
for text, x, y, xtext, ytext in annotations:
ax.annotate(text, xy=(x, y), xytext=(xtext, ytext), horizontalalignment="center",
arrowprops=dict(facecolor='slategrey', edgecolor='slategrey', shrink=0.05), fontsize=19)
texts = [(r'$e^-$+$e^+$', 0.5e2, 6e0),
(r'$e^+$', 0.85e1, 2e0),
(r'$\bar{p}$', 2.2, 1.4e-2),
(r'$p$', 7, 0.7e3),
(r'$\nu + \bar{\nu}$', 0.5e6, 2.5e-4),
(r'$\gamma$', 0.5e2, 1.8e-2),
(r'$\gamma$ IGRB', 0.9e3, 3e-5),
]
for text, x, y in texts:
ax.text(x, y, text, fontsize=20)
# Show N(>E) regions
E = np.logspace(-1, 12) # GeV
N = 1. # m-2 s-1
E2I = 1.7 * N * E / 4. / math.pi # m-2 s-1 GeV sr-1
ax.text(0.3e4, 0.8e3, r'1/m$^2$/s', fontsize=16, color='tab:gray', rotation=50)
ax.fill_between(E, E2I, 1e4, alpha=0.12, lw=0, facecolor='tab:gray', edgecolor='tab:gray')
N = 1. / 3.14e7 # m-2 yr-1
E2I = 1.7 * N * E / 4. / math.pi # m-2 s-1 GeV sr-1
ax.text(3.1e10, 0.3e3, r'1/m$^2$/yr', fontsize=16, color='tab:gray', rotation=50)
ax.fill_between(E, E2I, 1e4, alpha=0.12, lw=0, facecolor='tab:gray', edgecolor='tab:gray')
N = 1. / 3.14e7 / 1e6 # km-2 yr-1
E2I = 1.7 * N * E / 4. / math.pi # m-2 s-1 GeV sr-1
ax.text(2.1e10, 1.95e-4, r'1/km$^2$/yr', fontsize=16, color='tab:gray', rotation=50)
ax.fill_between(E, E2I, 1e4, alpha=0.12, lw=0, facecolor='tab:gray', edgecolor='tab:gray')
ax.fill_between(E, E2I, 1e-10, alpha=0.06, lw=0, facecolor='tab:gray', edgecolor='tab:gray')
# Add credits
ax.text(1.1e12, 2e-1, r'github.com/carmeloevoli/The_CR_Spectrum', rotation=-90, fontsize=11, color='tab:gray')
def experiment_legend(self, ax):
"""Adds legend for experiments."""
for i, (exp, color) in enumerate(self.colors.items()):
ax.text(1.1e9, self.ypos(i), exp, color=color, fontsize=13)
def ypos(self, i):
"""Returns vertical position for experiment labels."""
return 0.015 * pow(1.95, i)
def plot_experiment_data(self, ax, experiment_type):
"""Plot function to handle different types of particles (positrons, antiprotons, etc.)"""
data_files = {
'positrons': ['AMS-02_e+_energy.txt',
'FERMI_e+_energy.txt',
'PAMELA_e+_energy.txt'],
'antiprotons': ['AMS-02_pbar_energy.txt',
'BESS_pbar_energy.txt',
'PAMELA_pbar_energy.txt'],
'leptons': ['AMS-02_e-e+_energy.txt',
'CALET_e-e+_energy.txt',
'DAMPE_e-e+_energy.txt',
'FERMI_e-e+_energy.txt',
'HESS_e-e+_energy.txt'],
'protons': ['AMS-02_H_energy.txt',
'BESS_H_energy.txt',
'CREAM_H_energy.txt',
'CALET_H_energy.txt',
'DAMPE_H_energy.txt',
'KASCADE_H_energy.txt',
'KASCADE-Grande_H_energy.txt',
'PAMELA_H_energy.txt'],
'allParticles' : ['AUGER_allParticles_energy.txt',
'HAWC_allParticles_energy.txt',
'KASCADE_allParticles_energy.txt',
'KASCADE-Grande_allParticles_energy.txt',
'NUCLEON_allParticles_energy.txt',
'IceCube_allParticles_energy.txt',
'TA_allParticles_energy.txt',
'TUNKA-133_allParticles_energy.txt'],
}
pdir = 'data/crdb/'
if experiment_type in data_files:
for filename in data_files[experiment_type]:
self.plot_data(ax, f'{pdir}{filename}', 'o', self.colors[filename.split('_')[0]], 1)
if experiment_type == 'allParticles':
self.plot_line(ax, f'{pdir}AMS-02_allParticles_energy.txt', self.colors['AMS-02'])
self.plot_line(ax, f'{pdir}CREAM_allParticles_energy.txt', self.colors['CREAM'])
def neutrinos(self, ax):
"""Plot neutrino measurements with error bars."""
filename = 'data/tables/IceCube_nus_energy.txt'
self.plot_data_diffuse(ax, filename, self.colors['IceCube'])
def gammas(self, ax):
"""Plot ... with error bars."""
filename = 'data/tables/FERMI_igrb_energy.txt'
self.plot_data_diffuse(ax, filename, self.colors['FERMI'])
filename = 'data/tables/FERMI_inner_energy.txt'
self.plot_data_diffuse(ax, filename, self.colors['FERMI'])
def plot_data(self, ax, filename, fmt, color, zorder=1):
"""Plot data with error bars."""
E, dJdE, errStatLo, errStatUp, errSysLo, errSysUp = np.loadtxt(
filename, skiprows=8, usecols=(0, 1, 2, 3, 4, 5), unpack=True
)
E2 = E * E
y = E2 * dJdE
dyLo = E2 * np.sqrt(errStatLo**2 + errSysLo**2)
dyUp = E2 * np.sqrt(errStatUp**2 + errSysUp**2)
ind = dyLo < y
ax.errorbar(E[ind], y[ind], yerr=[dyLo[ind], dyUp[ind]], fmt=fmt, markeredgecolor=color,
color=color, elinewidth=1.5, capthick=1.5, zorder=zorder)
ind_upper = dyLo > y
ax.errorbar(E[ind_upper], y[ind_upper], yerr=0.25 * y[ind_upper], uplims=True,
fmt=fmt, markeredgecolor=color, color=color, elinewidth=1.5, capthick=1.5, zorder=zorder)
def plot_line(self, ax, filename, color):
"""Plot a line for the all-particle spectrum."""
E, dJdE = np.loadtxt(filename, usecols=(0, 1), unpack=True)
ax.plot(E, E**2 * dJdE, color=color)
def plot_data_diffuse(self, ax, filename, color):
"""Plot diffuse data."""
x, dxLo, dxUp, y, dyLo, dyUp = np.loadtxt(filename, skiprows=1, usecols=(0, 1, 2, 3, 4, 5), unpack=True)
ind = dyLo < y
ax.errorbar(x[ind], y[ind], yerr=[dyLo[ind], dyUp[ind]], xerr=[dxLo[ind], dxUp[ind]],
fmt='o', markeredgecolor=color, color=color, elinewidth=1.5, capthick=1.5, mfc='white')
ind_upper = dyLo > y
ax.errorbar(x[ind_upper], y[ind_upper], xerr=[dxLo[ind_upper], dxUp[ind_upper]], yerr=0.25 * y[ind_upper], uplims=True,
fmt='o', markeredgecolor=color, color=color, elinewidth=1.5, capthick=1.5, mfc='white')