-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__plotUtility__.py
More file actions
233 lines (170 loc) · 8.69 KB
/
Copy path__plotUtility__.py
File metadata and controls
233 lines (170 loc) · 8.69 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import arviz as az
import corner
# Define custom color list with colorblind friendlyness for cornerplots
color_list = ['none', '#7acfff', '#00c48f', '#0093e6']
# Matplotlib preferences
plt.rc('font', size=14) # controls default text sizes
plt.rc('axes', titlesize=16) # fontsize of the axes title
plt.rc('axes', labelsize=16) # fontsize of the x and y labels
plt.rc('xtick', labelsize=16) # fontsize of the tick labels
plt.rc('ytick', labelsize=16) # fontsize of the tick labels
plt.rc('legend', fontsize=16) # legend fontsize
plt.rc('figure', titlesize=16) # fontsize of the figure title
plt.rc('figure', dpi=75) # Changed to 100 for some version >3.5.2
plt.rc('axes', grid=True) # Default set grids to true
########################################
########################################
### P L O T T I N G U T I L I T Y ###
########################################
########################################
# function for basic plotting using different colors and markers by source
def plot_data(ax, data, plotDict, axisDict, plotSettings, interpolateDict, legendCol=2, legendLoc='best'):
# Extract relevant keys
key_x = plotDict['x']
key_y = plotDict['y']
key_xerr = plotDict['x_err']
key_yerr = plotDict['y_err']
# Loop over entries
for index in range(len(data)):
# Makes sure not to get duplicate labels
if data['source'][index] not in ax.get_legend_handles_labels()[1]:
myLabel = data['source'][index]
else:
myLabel = ''
# Interpolation of function (used for residuals)
fitInterp = 0
if interpolateDict['subtract']:
fitInterp = np.interp(data[key_x][index], interpolateDict['x_linspace'], interpolateDict['function'])
# Relevant data sets to plot
xval = data[key_x][index]
yval = data[key_y][index] - fitInterp
xerr = data[key_xerr][index]
yerr = data[key_yerr][index]
# Plot data points
if interpolateDict['ratio']:
yval = 100 * (yval/fitInterp) # Change of difference to relative difference
yerr = 100 * (yerr/fitInterp)
ax.errorbar(xval, yval, xerr = xerr, yerr = yerr,
marker = plotSettings['markers'][data['source'][index]], color = plotSettings['colors'][data['source'][index]],
markersize = plotSettings['markersizes'][data['source'][index]], label = myLabel)
# Axis and legend
ax.legend(loc=legendLoc, ncol=legendCol)
#ax.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
ax.set_xlabel(axisDict['label_x'])
ax.set_ylabel(axisDict['label_y'])
if axisDict['range_x'] != 0:
ax.set_xlim(axisDict['range_x'][0], axisDict['range_x'][1])
if axisDict['range_y'] != 0:
ax.set_ylim(axisDict['range_y'][0], axisDict['range_y'][1])
# Plot the log of eff with potentially a fit
def plot_logEff(ax, data, fitDict, plotSettings, legendCol=3, legendLoc='best'):
# Prepare dictionaries to pass to plotData
plotDict = {
'x' : 'E', 'x_err' : 'E_err',
'y' : 'logeff', 'y_err' : 'logeff_err'
}
interpolateDict = {
'subtract' : False, 'ratio': False, 'function' : 0, 'x_linspace' : 0
}
min_y = min(data[plotDict['y']])
max_y = max(data[plotDict['y']])
axisDict = {
'label_x' : "Energy (keV)", 'range_x' : 0,
'label_y' : r'$log(\varepsilon)$', 'range_y' : [min_y - 0.2*abs(max_y-min_y), max_y + 0.2*abs(max_y-min_y)]
}
# If requested, also show fit and confidence interval
if fitDict['show']:
ax.plot(fitDict['xvals'], fitDict['yvals'], 'r-', label="fit")
ax.fill_between(fitDict['xvals'], fitDict['lower'], fitDict['upper'], color="grey", alpha=0.2, label=r'$1\sigma$')
plot_data(ax, data, plotDict, axisDict, plotSettings, interpolateDict, legendCol=legendCol, legendLoc=legendLoc)
# Plot the eff with potentially a fit
def plot_eff(ax, data, fitDict, plotSettings, legendCol=3, legendLoc='best', scaled=False):
# Prepare dictionaries to pass to plotData
plotDict = {
'x' : 'E', 'x_err' : 'E_err',
'y' : 'eff', 'y_err' : 'eff_err'
}
if scaled:
plotDict['y'] = 'recalc_eff'
plotDict['y_err'] = 'recalc_eff_err'
axisDict = {
'label_x' : "Energy (keV)", 'range_x' : 0,
'label_y' : r'$\varepsilon$', 'range_y' : [0, 1.2*max(data['eff'])]
}
interpolateDict = {
'subtract' : False, 'ratio': False, 'function' : 0, 'x_linspace' : 0
}
# If requested, also show fit and confidence interval
if fitDict['show']:
ax.plot(fitDict['xvals'], fitDict['yvals'], 'r-', label="fit")
ax.fill_between(fitDict['xvals'], fitDict['lower'], fitDict['upper'], color="grey", alpha=0.2, label=r'$1\sigma$')
plot_data(ax, data, plotDict, axisDict, plotSettings, interpolateDict, legendCol=legendCol, legendLoc=legendLoc)
# Plot the relative residuals compared to the fit function (value/fit - 1)
def plot_relResid_eff(ax, data, fitDict, plotSettings, legendCol=3, legendLoc='best', scaled=False):
# Prepare dictionaries to pass to plotData
plotDict = {
'x' : 'E', 'x_err' : 'E_err',
'y' : 'eff', 'y_err' : 'eff_err'
}
if scaled:
plotDict['y'] = 'recalc_eff'
plotDict['y_err'] = 'recalc_eff_err'
interpolateDict = { # Subtract fit to get residuals
'subtract' : True, 'ratio': True, 'x_linspace' : fitDict['xvals'], 'function' : fitDict['yvals']
}
interp = np.interp(data[plotDict['x']], interpolateDict['x_linspace'], interpolateDict['function'])
values = 100 * (data[plotDict['y']] - interp)/interp
min_y = min(values)
max_y = max(values)
#min_y = min(data[plotDict['y']] - np.interp(data[plotDict['x']], interpolateDict['x_linspace'], interpolateDict['function']))
#max_y = max(data[plotDict['y']] - np.interp(data[plotDict['x']], interpolateDict['x_linspace'], interpolateDict['function']))
axisDict = {
'label_x' : "Energy (keV)", 'range_x' : 0,
'label_y' : 'Relative residual (%)', 'range_y' : [min_y - 1.0*abs(max_y-min_y), max_y + 1.0*abs(max_y-min_y)]
}
# If requested, draw confidence band
if fitDict['show']:
ax.fill_between(fitDict['xvals'], 100*(fitDict['lower'] - fitDict['yvals'])/fitDict['yvals'],
100*(fitDict['upper'] - fitDict['yvals'])/fitDict['yvals'], color="grey", alpha=0.2, label=r'$1\sigma$')
plot_data(ax, data, plotDict, axisDict, plotSettings, interpolateDict, legendCol=legendCol, legendLoc=legendLoc)
ax.legend().set_visible(False)
# Make a nice cornerplot
def corner_plot(trace, var_names, N, figsize=(12, 12)):
fig, axes = plt.subplots(N, N, figsize=figsize)
fig = corner.corner(trace, fig=fig, var_names=var_names,
plot_datapoints=True, plot_density=False, plot_contours=True,
levels=[0.6827, 0.9545, 0.9973], smooth=0.8,
quantiles=[0.159, 0.5, 0.841], bins=50,
fill_contours=True, contourf_kwargs={'colors': color_list},
show_titles=True, title_kwargs={'fontsize': 12}, title_fmt='.1e')
axes = fig.get_axes()
for i, ax in enumerate(axes):
row, col = divmod(i, N) # Determine the row and column of the subplot
# Apply labels and scientific notation only to the left column (y-axis)
if col == 0:
ax.yaxis.set_major_formatter(ticker.ScalarFormatter())
ax.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
ax.yaxis.offsetText.set_position((-0.15, 0.9)) # Adjust as needed
ax.yaxis.offsetText.set_rotation(0)
ax.yaxis.set_label_coords(-0.5, 0.5)
else:
ax.set_yticklabels([]) # Remove y tick labels for other columns
# Apply labels and scientific notation only to the bottom row (x-axis)
if row == N - 1:
ax.xaxis.set_major_formatter(ticker.ScalarFormatter())
ax.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
ax.xaxis.set_label_coords(0.5, -0.5)
ax.xaxis.offsetText.set_position((1.1, 0.)) # Adjust as needed
ax.xaxis.offsetText.set_rotation(90)
else:
ax.set_xticklabels([])
#for ax in fig.get_axes():
# ax.xaxis.set_label_coords(0.5, -0.5)
# ax.yaxis.set_label_coords(-0.4, 0.5)
plt.gcf().subplots_adjust(left=0.175)
plt.gcf().subplots_adjust(bottom=0.18)
plt.subplots_adjust(wspace=0.225, hspace=0.225, right=0.875)
#plt.tight_layout()