def test_model_plot(): pars, errs = {}, {} pars["amplitude"] = 1e-12 * u.Unit("TeV-1 cm-2 s-1") pars["reference"] = 1 * u.Unit("TeV") pars["index"] = 2 * u.Unit("") errs["amplitude"] = 0.1e-12 * u.Unit("TeV-1 cm-2 s-1") pwl = PowerLaw(**pars) pwl.parameters.set_parameter_errors(errs) with mpl_plot_check(): pwl.plot((1 * u.TeV, 10 * u.TeV)) with mpl_plot_check(): pwl.plot_error((1 * u.TeV, 10 * u.TeV))
# And print the result: # In[ ]: print(result_pwl) # In[ ]: print(pwl) # Finally we plot the data points and the best fit model: # In[ ]: ax = flux_points.plot(energy_power=2) pwl.plot(energy_range=[1e-4, 1e2] * u.TeV, ax=ax, energy_power=2) # assign covariance for plotting pwl.parameters.covariance = result_pwl.parameters.covariance pwl.plot_error(energy_range=[1e-4, 1e2] * u.TeV, ax=ax, energy_power=2) ax.set_ylim(1e-13, 1e-11) # ## Exponential Cut-Off Powerlaw Fit # # Next we fit an [exponential cut-off power](https://docs.gammapy.org/0.11/api/gammapy.spectrum.models.ExponentialCutoffPowerLaw.html#gammapy.spectrum.models.ExponentialCutoffPowerLaw) law to the data. # In[ ]: ecpl = ExponentialCutoffPowerLaw( index=1.8,
# First plot for the spectral flux ax0, ax1 = spectrum_result.plot( energy_range=joint_fit.result[0].fit_range, fig_kwargs=dict(figsize=(8, 8)), point_kwargs=dict(label="Flux points"), fit_kwargs=dict(label="Gammapy fit"), ) ax0.set_ylim([1e-14, 1e-7]) ax0.set_xlim([4e-2, 5e-1]) ax1.set_ylim([-1.7, 1.7]) spec_model_true.plot( ax=ax0, energy_range=joint_fit.result[0].fit_range, label="Reference model", c="black", linestyle="dashed", ) ax0.legend(loc="best") ax0.set_ylabel(r"Flux [cm$^{-2}$ s$^1$ TeV$^{-1}$]", size=14) ax1.set_ylabel("Residuals", size=14) ax1.set_xlabel("Energy [TeV]", size=14) ax1.set_xticks([5e-2, 1e-1, 3e-1]) ax1.set_xticklabels([5e-2, 1e-1, 3e-1]) # In[ ]: # Second plot for the spectral energy flux
edisp=edisp, source_model=model, livetime=livetime) sim.simulate_obs(seed=42, obs_id=0) sim.obs.peek() print sim.obs plt.show() fit = SpectrumFit(obs_list=sim.obs, model=model, stat='cash') fit.run() result = fit.result[0] print result energy_range = [0.1, 100] * u.TeV model.plot(energy_range=energy_range, energy_power=2) result.model.plot(energy_range=energy_range, energy_power=2) result.model.plot_error(energy_range=energy_range, energy_power=2) plt.show() #now importing a background #from a background model bkg_index = 2.5 * u.Unit('') bkg_amplitude = 1e-11 * u.Unit('cm-2 s-1 TeV-1') reference = 1 * u.TeV bkg_model = PowerLaw(index=bkg_index, amplitude=bkg_amplitude, reference=reference)
amplitude_ref = 0.57 * 19.4e-14 * u.Unit("1 / (cm2 s MeV)") spec_model_true = PowerLaw(index=4.5, amplitude=amplitude_ref, reference="20 GeV") flux_points_dataset = FluxPointsDataset(data=flux_points, model=model) # Now we can plot. # In[ ]: plt.figure(figsize=(8, 6)) ax_spectrum, ax_residual = flux_points_dataset.peek() ax_spectrum.set_ylim([1e-14, 3e-11]) ax_residual.set_ylim([-1.7, 1.7]) spec_model_true.plot( ax=ax_spectrum, energy_range=(emin_fit, emax_fit), label="Reference model", c="black", linestyle="dashed", energy_power=2, ) ax_spectrum.legend(loc="best") # This tutorial suffers a bit from the lack of statistics: there were 9 Vela observations in the CTA DC1 while there is only one here. When done on the 9 observations, the spectral analysis is much better agreement between the input model and the gammapy fit.