def plot_points(self, axes, **kwargs): """ Plot the SED using matpotlib. """ data_kwargs = dict(color='black') data_kwargs.update(kwargs) edict = self.results['Energy'] fdict = self.results['dNdE'] file_energy_units = units.fromstring(edict['Units']) file_flux_units = units.fromstring(fdict['Units']) ce = lambda x: units.convert(np.asarray(x), file_energy_units, axes. energy_units_obj) # get energy part energy = ce(edict['Value']) if 'Lower' in edict and 'Upper' in edict: lower_energy = ce(edict['Lower']) upper_energy = ce(edict['Upper']) has_energy_errors = True else: has_energy_errors = False # get spectral part cf = lambda y: units.convert( energy**2 * np.asarray(y), axes.energy_units_obj**2 * file_flux_units, axes.flux_units_obj / units.cm**2 / units.s) dnde = cf(fdict['Value']) if 'Lower_Error' in fdict and 'Upper_Error' in fdict: # assymetric errors dnde_lower_err = cf(fdict['Lower_Error']) dnde_upper_err = cf(fdict['Upper_Error']) has_assymetric_errors = True else: has_assymetric_errors = False dnde_err = cf(fdict['Average_Error']) # get limits, otherwise assume all significant if 'Upper_Limit' in fdict and 'Significant' in self.results: dnde_ul = cf(fdict['Upper_Limit']) significant = np.asarray(self.results['Significant']) has_upper_limits = True else: has_upper_limits = False plot_points( x=energy, xlo=lower_energy if has_energy_errors else None, xhi=upper_energy if has_energy_errors else None, y=dnde, y_lower_err=dnde_lower_err if has_assymetric_errors else dnde_err, y_upper_err=dnde_upper_err if has_assymetric_errors else dnde_err, y_ul=dnde_ul if has_upper_limits else None, significant=significant if has_upper_limits else np.ones(len(energy), dtype=bool), axes=axes, **data_kwargs)
def plot_points(self, axes, **kwargs): """ Plot the SED using matpotlib. """ data_kwargs=dict(color='black') data_kwargs.update(kwargs) edict = self.results['Energy'] fdict = self.results['dNdE'] file_energy_units = units.fromstring(edict['Units']) file_flux_units = units.fromstring(fdict['Units']) ce = lambda x: units.convert(np.asarray(x),file_energy_units, axes.energy_units_obj) # get energy part energy = ce(edict['Value']) if 'Lower' in edict and 'Upper' in edict: lower_energy = ce(edict['Lower']) upper_energy = ce(edict['Upper']) has_energy_errors = True else: has_energy_errors = False # get spectral part cf = lambda y: units.convert(energy**2*np.asarray(y), axes.energy_units_obj**2*file_flux_units, axes.flux_units_obj/units.cm**2/units.s) dnde = cf(fdict['Value']) if 'Lower_Error' in fdict and 'Upper_Error' in fdict: # assymetric errors dnde_lower_err = cf(fdict['Lower_Error']) dnde_upper_err = cf(fdict['Upper_Error']) has_assymetric_errors = True else: has_assymetric_errors = False dnde_err = cf(fdict['Average_Error']) # get limits, otherwise assume all significant if 'Upper_Limit' in fdict and 'Significant' in self.results: dnde_ul = cf(fdict['Upper_Limit']) significant = np.asarray(self.results['Significant']) has_upper_limits=True else: has_upper_limits=False plot_points( x=energy, xlo=lower_energy if has_energy_errors else None, xhi=upper_energy if has_energy_errors else None, y=dnde, y_lower_err=dnde_lower_err if has_assymetric_errors else dnde_err, y_upper_err=dnde_upper_err if has_assymetric_errors else dnde_err, y_ul=dnde_ul if has_upper_limits else None, significant=significant if has_upper_limits else np.ones(len(energy),dtype=bool), axes=axes, **data_kwargs)
def plot_stat(cut, **kwargs): # plot PWN print 'kwargs',kwargs plot_points(Edot[cut], luminosity[cut], xlo=None, xhi=None, y_lower_err=luminosity_error_statistical[cut], y_upper_err=luminosity_error_statistical[cut], y_ul=luminosity_ul[cut], significant=luminosity_significant[cut], axes=axes, **kwargs)
def plot_sys(cut, **kwargs): # plot PWN print 'sys: kwargs',kwargs print luminosity[cut],luminosity_lower_error_systematic[cut],luminosity_upper_error_systematic[cut], plot_points(Edot[cut], luminosity[cut], xlo=None, xhi=None, y_lower_err=luminosity_lower_error_systematic[cut], y_upper_err=luminosity_upper_error_systematic[cut], y_ul=luminosity_ul[cut], significant=luminosity_significant[cut], axes=axes, **kwargs)
def plot(self, filename=None, axes=None, title=None, fignum=None, figsize=(4,4), **kwargs): """ Plot the upper limit. """ spectral_kwargs=dict(color='red',zorder=1.9) spectral_kwargs.update(kwargs) file_energy_units = units.fromstring(self.results['energy_units']) emin = self.results['emin']*file_energy_units emax = self.results['emax']*file_energy_units if axes is None: fig = P.figure(fignum,figsize) axes = SpectralAxes(fig=fig, rect=(0.22,0.15,0.75,0.8), flux_units=self.flux_units, energy_units=self.energy_units) fig.add_axes(axes) axes.set_xlim_units(emin,emax) # plot the spectral model spectrum = self.results['spectrum'] sp=SpectrumPlotter(axes=axes) sp.plot(spectrum, emin=emin, emax=emax, **spectral_kwargs) # plot in the middle an arrow pointing down e_middle = units.tosympy([np.sqrt(self.results['emin']*self.results['emax'])],file_energy_units) dnde = sp.get_dnde(spectrum, e_middle) energies, e2_dnde = axes.convert_points(e_middle, dnde) if 'autoscale' in spectral_kwargs: spectral_kwargs.pop('autoscale') if 'label' in spectral_kwargs: spectral_kwargs.pop('label') plot_points(x=energies, y=e2_dnde, xlo=None, xhi=None, y_lower_err=None, y_upper_err=None, y_ul=e2_dnde, significant=False, axes=axes, **spectral_kwargs) if title is not None: axes.set_title(title) if filename is not None: P.savefig(expandvars(filename)) return axes
def plot(self, filename=None, axes=None, figsize=(7,4), plot_gtlike=True, plot_pointlike=True, gtlike_color='black', pointlike_color='red', **kwargs): """ Create a plot from a dictionary. """ bands = self.results['bands'] all_time = self.results['all_time'] min_ts = self.results['min_ts'] if axes is None: fig = P.figure(None,figsize) axes = fig.add_subplot(111) # astype(float) converts None to nan a=lambda x: np.asarray(x).astype(float) starts=a(self.results['time']['starts']) stops=a(self.results['time']['stops']) time=(starts+stops)/2.0 loop = [ ] if plot_gtlike: loop.append(['gtlike',gtlike_color],) if plot_pointlike: loop.append(['pointlike',pointlike_color]) for t,color in loop: f0 = all_time[t]['flux']['flux'] ts = a([b[t]['TS'] for b in bands]) significant = (ts >= min_ts) f = a([b[t]['flux']['flux'] for b in bands]) ferr=a([b[t]['flux']['flux_err'] for b in bands]) ferr=a([b[t]['flux']['flux_err'] for b in bands]) fup=a([b[t]['upper_limit']['flux'] if b[t]['upper_limit'] is not None else None for b in bands]) plot_points( axes=axes, x=time, xlo=starts, xhi=stops, y=f, y_lower_err=ferr, y_upper_err=ferr, y_ul=fup, significant=significant, color=color, label=t, **kwargs) axes.axhline(f0, color=color, dashes=[5,2]) axes.set_xlabel('MET (s)') axes.set_ylabel('Flux (ph$\,$cm$^{-2}$s$^{-1}$)') axes.legend() if filename is not None: P.savefig(expandvars(filename)) return axes
def plot(self, filename=None, axes=None, figsize=(7, 4), plot_gtlike=True, plot_pointlike=True, gtlike_color='black', pointlike_color='red', **kwargs): """ Create a plot from a dictionary. """ bands = self.results['bands'] all_time = self.results['all_time'] min_ts = self.results['min_ts'] if axes is None: fig = P.figure(None, figsize) axes = fig.add_subplot(111) # astype(float) converts None to nan a = lambda x: np.asarray(x).astype(float) starts = a(self.results['time']['starts']) stops = a(self.results['time']['stops']) time = (starts + stops) / 2.0 loop = [] if plot_gtlike: loop.append(['gtlike', gtlike_color], ) if plot_pointlike: loop.append(['pointlike', pointlike_color]) for t, color in loop: f0 = all_time[t]['flux']['flux'] ts = a([b[t]['TS'] for b in bands]) significant = (ts >= min_ts) f = a([b[t]['flux']['flux'] for b in bands]) ferr = a([b[t]['flux']['flux_err'] for b in bands]) ferr = a([b[t]['flux']['flux_err'] for b in bands]) fup = a([ b[t]['upper_limit']['flux'] if b[t]['upper_limit'] is not None else None for b in bands ]) plot_points(axes=axes, x=time, xlo=starts, xhi=stops, y=f, y_lower_err=ferr, y_upper_err=ferr, y_ul=fup, significant=significant, color=color, label=t, **kwargs) axes.axhline(f0, color=color, dashes=[5, 2]) axes.set_xlabel('MET (s)') axes.set_ylabel('Flux (ph$\,$cm$^{-2}$s$^{-1}$)') axes.legend() if filename is not None: P.savefig(expandvars(filename)) return axes