Commit 2aed4a877d23b8f7c64bb7d48d6a401529d07f8d

Authored by Josh Cardenzana
1 parent edcb620c

Cleanup example script 'show_spectrum.py' (#2938)

Showing 1 changed file with 19 additions and 19 deletions
examples/show_spectrum.py
... ... @@ -176,30 +176,30 @@ def plot_dloglike(spec):
176 176 """
177 177 # Establish the bounds on the x,y plot
178 178 ymin,ymax = plt.gca().get_ylim()
179   - ymin = np.log10(ymin)
180   - ymax = np.log10(ymax)
181   - steps = 1000
182   - y_bounds = np.linspace(ymin, ymax, steps+1)
183   - fluxpnts = []
184   - for i in range(steps):
185   - mid_pnt = (y_bounds[i] + y_bounds[i+1]) / 2.0
186   - fluxpnts.append( 10 ** mid_pnt )
187   - # Scale the points
  179 + ymin = np.log10(ymin)
  180 + ymax = np.log10(ymax)
  181 + steps = 1000
  182 + ebins = len(spec['e2dnde_scan'])
  183 + y_bounds = np.linspace(ymin, ymax, steps+1)
  184 +
  185 + # Compute the logarithmic center of each bin
  186 + fluxpnts = 10 ** ((y_bounds[1:]+y_bounds[:-1]) / 2.0)
  187 +
  188 + # Scale the boundaries on the grid
188 189 y_bounds = 10 ** y_bounds
189 190  
190   - dll_hist = []
191   - for ebin in range(len(spec['e2dnde_scan'])):
192   -
193   - # Interpolate the dlogLike values
194   - dll_interp = np.interp(fluxpnts, spec['e2dnde_scan'][ebin],
195   - spec['dll_scan'][ebin])
196   - dll_hist.append(dll_interp)
  191 + # Interpolate the dlogLike values
  192 + dll_hist = [[]]*ebins
  193 + for ebin in range(ebins):
  194 + dll_hist[ebin] = np.interp(fluxpnts, spec['e2dnde_scan'][ebin],
  195 + spec['dll_scan'][ebin])
197 196  
198   - # Transpose the dll_hist array
199   - dll_hist = [[row[i] for row in dll_hist] for i in range(len(dll_hist[0]))]
  197 + # Transpose the dll_hist array so x=energy, y=dnde
  198 + dll_hist = [[row[i] for row in dll_hist] for i in range(steps)]
200 199  
  200 + # Plot the likelihood profiles
201 201 plt.pcolormesh(spec['engs_scan'], y_bounds, dll_hist, vmin=-10.0, vmax=0.0,
202   - cmap='Reds', alpha=0.9)
  202 + cmap='Reds', alpha=0.9)
203 203 cbar = plt.colorbar()
204 204 cbar.set_label(r'$\Delta$ log-likelihood')
205 205 plt.grid()
... ...