def test_model_plot_sed_type(): pwl = PowerLawSpectralModel( amplitude=1e-12 * u.Unit("TeV-1 cm-2 s-1"), reference=1 * u.Unit("TeV"), index=2 ) pwl.amplitude.error = 0.1e-12 * u.Unit("TeV-1 cm-2 s-1") with mpl_plot_check(): ax1 = pwl.plot((1 * u.TeV, 100 * u.TeV), sed_type="dnde") ax2 = pwl.plot_error((1 * u.TeV, 100 * u.TeV), sed_type="dnde") assert ax1.axes.axes.get_ylabel() == "dnde [1 / (cm2 s TeV)]" assert ax2.axes.axes.get_ylabel() == "dnde [1 / (cm2 s TeV)]" with mpl_plot_check(): ax1 = pwl.plot((1 * u.TeV, 100 * u.TeV), sed_type="e2dnde") ax2 = pwl.plot_error((1 * u.TeV, 100 * u.TeV), sed_type="e2dnde") assert ax1.axes.axes.get_ylabel() == "e2dnde [erg / (cm2 s)]" assert ax2.axes.axes.get_ylabel() == "e2dnde [erg / (cm2 s)]" with mpl_plot_check(): ax1 = pwl.plot((1 * u.TeV, 100 * u.TeV), sed_type="flux") ax2 = pwl.plot_error((1 * u.TeV, 100 * u.TeV), sed_type="flux") assert ax1.axes.axes.get_ylabel() == "flux [1 / (cm2 s)]" assert ax2.axes.axes.get_ylabel() == "flux [1 / (cm2 s)]" with mpl_plot_check(): ax1 = pwl.plot((1 * u.TeV, 100 * u.TeV), sed_type="eflux") ax2 = pwl.plot_error((1 * u.TeV, 100 * u.TeV), sed_type="eflux") assert ax1.axes.axes.get_ylabel() == "eflux [erg / (cm2 s)]" assert ax2.axes.axes.get_ylabel() == "eflux [erg / (cm2 s)]"
def test_model_plot(): pwl = PowerLawSpectralModel(amplitude=1e-12 * u.Unit("TeV-1 cm-2 s-1"), reference=1 * u.Unit("TeV"), index=2) pwl.parameters.set_error(amplitude=0.1e-12 * u.Unit("TeV-1 cm-2 s-1")) 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))
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 = PowerLawSpectralModel(**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))
""" # %% # Example plot # ------------ # Here is an example plot of the model: from astropy import units as u import matplotlib.pyplot as plt from gammapy.modeling.models import Models, PowerLawSpectralModel, SkyModel energy_range = [0.1, 100] * u.TeV model = PowerLawSpectralModel( index=2, amplitude="1e-12 TeV-1 cm-2 s-1", reference=1 * u.TeV, ) model.plot(energy_range) plt.grid(which="both") # %% # YAML representation # ------------------- # Here is an example YAML file using the model: model = SkyModel(spectral_model=model, name="power-law-model") models = Models([model]) print(models.to_yaml())
# 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. # In[ ]:
# 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 `~gammapy.modeling.models.ExpCutoffPowerLawSpectralModel` law to the data. # In[ ]: ecpl = ExpCutoffPowerLawSpectralModel( index=1.8,
""" # %% # Example plot # ------------ # Here is an example plot of the model: from astropy import units as u import matplotlib.pyplot as plt from gammapy.modeling.models import Models, PowerLawSpectralModel, SkyModel energy_bounds = [0.1, 100] * u.TeV model = PowerLawSpectralModel( index=2, amplitude="1e-12 TeV-1 cm-2 s-1", reference=1 * u.TeV, ) model.plot(energy_bounds) plt.grid(which="both") # %% # YAML representation # ------------------- # Here is an example YAML file using the model: model = SkyModel(spectral_model=model, name="power-law-model") models = Models([model]) print(models.to_yaml())