.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "user-guide/model-gallery/spatial/plot_disk.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_user-guide_model-gallery_spatial_plot_disk.py: .. _disk-spatial-model: Disk spatial model ================== This is a spatial model parametrising a disk. By default, the model is symmetric, i.e. a disk: .. math:: \phi(lon, lat) = \frac{1}{2 \pi (1 - \cos{r_0}) } \cdot \begin{cases} 1 & \text{for } \theta \leq r_0 \\ 0 & \text{for } \theta > r_0 \end{cases} where :math:`\theta` is the sky separation. To improve fit convergence of the model, the sharp edges is smoothed using `~scipy.special.erf`. In case an eccentricity (`e`) and rotation angle (:math:`\phi`) are passed, then the model is an elongated disk (i.e. an ellipse), with a major semiaxis of length :math:`r_0` and position angle :math:`\phi` (increasing counter-clockwise from the North direction). The model is defined on the celestial sphere, with a normalization defined by: .. math:: \int_{4\pi}\phi(\text{lon}, \text{lat}) \,d\Omega = 1\,. .. GENERATED FROM PYTHON SOURCE LINES 34-37 Example plot ------------ Here is an example plot of the model: .. GENERATED FROM PYTHON SOURCE LINES 37-74 .. code-block:: Python import numpy as np from astropy.coordinates import Angle from gammapy.modeling.models import ( DiskSpatialModel, Models, PowerLawSpectralModel, SkyModel, ) phi = Angle("30 deg") model = DiskSpatialModel( lon_0="2 deg", lat_0="2 deg", r_0="1 deg", e=0.8, phi=phi, edge_width=0.1, frame="galactic", ) ax = model.plot(add_cbar=True) # illustrate size parameter region = model.to_region().to_pixel(ax.wcs) artist = region.as_artist(facecolor="none", edgecolor="red") ax.add_artist(artist) transform = ax.get_transform("galactic") ax.scatter(2, 2, transform=transform, s=20, edgecolor="red", facecolor="red") ax.text(1.7, 1.85, r"$(l_0, b_0)$", transform=transform, ha="center") ax.plot([2, 2 + np.sin(phi)], [2, 2 + np.cos(phi)], color="r", transform=transform) ax.vlines(x=2, color="r", linestyle="--", transform=transform, ymin=0, ymax=5) ax.text(2.15, 2.3, r"$\phi$", transform=transform) .. image-sg:: /user-guide/model-gallery/spatial/images/sphx_glr_plot_disk_001.png :alt: plot disk :srcset: /user-guide/model-gallery/spatial/images/sphx_glr_plot_disk_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(2.15, 2.3, '$\\phi$') .. GENERATED FROM PYTHON SOURCE LINES 75-76 This plot illustrates the definition of the edge parameter: .. GENERATED FROM PYTHON SOURCE LINES 76-115 .. code-block:: Python import numpy as np from astropy import units as u from astropy.visualization import quantity_support import matplotlib.pyplot as plt from gammapy.modeling.models import DiskSpatialModel lons = np.linspace(0, 0.3, 500) * u.deg r_0, edge_width = 0.2 * u.deg, 0.5 disk = DiskSpatialModel(lon_0="0 deg", lat_0="0 deg", r_0=r_0, edge_width=edge_width) profile = disk(lons, 0 * u.deg) plt.plot(lons, profile / profile.max(), alpha=0.5) plt.xlabel("Radius (deg)") plt.ylabel("Profile (A.U.)") edge_min, edge_max = r_0 * (1 - edge_width / 2.0), r_0 * (1 + edge_width / 2.0) with quantity_support(): plt.vlines([edge_min, edge_max], 0, 1, linestyles=["--"], color="k") plt.annotate( "", xy=(edge_min, 0.5), xytext=(edge_min + r_0 * edge_width, 0.5), arrowprops=dict(arrowstyle="<->", lw=2), ) plt.text(0.2, 0.53, "Edge width", ha="center", size=12) margin = 0.02 * u.deg plt.hlines( [0.95], edge_min - margin, edge_min + margin, linestyles=["-"], color="k" ) plt.text(edge_min + margin, 0.95, "95%", size=12, va="center") plt.hlines( [0.05], edge_max - margin, edge_max + margin, linestyles=["-"], color="k" ) plt.text(edge_max - margin, 0.05, "5%", size=12, va="center", ha="right") plt.show() .. image-sg:: /user-guide/model-gallery/spatial/images/sphx_glr_plot_disk_002.png :alt: plot disk :srcset: /user-guide/model-gallery/spatial/images/sphx_glr_plot_disk_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 116-119 YAML representation ------------------- Here is an example YAML file using the model: .. GENERATED FROM PYTHON SOURCE LINES 119-126 .. code-block:: Python pwl = PowerLawSpectralModel() gauss = DiskSpatialModel() model = SkyModel(spectral_model=pwl, spatial_model=gauss, name="pwl-disk-model") models = Models([model]) print(models.to_yaml()) .. rst-class:: sphx-glr-script-out .. code-block:: none components: - name: pwl-disk-model type: SkyModel spectral: type: PowerLawSpectralModel parameters: - name: index value: 2.0 - name: amplitude value: 1.0e-12 unit: cm-2 s-1 TeV-1 - name: reference value: 1.0 unit: TeV spatial: type: DiskSpatialModel frame: icrs parameters: - name: lon_0 value: 0.0 unit: deg - name: lat_0 value: 0.0 unit: deg - name: r_0 value: 1.0 unit: deg - name: e value: 0.0 - name: phi value: 0.0 unit: deg - name: edge_width value: 0.01 metadata: creator: Gammapy 1.3 date: '2024-11-26T10:08:40.967864' origin: null .. _sphx_glr_download_user-guide_model-gallery_spatial_plot_disk.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/gammapy/gammapy-webpage/v1.3?urlpath=lab/tree/notebooks/1.3/user-guide/model-gallery/spatial/plot_disk.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_disk.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_disk.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_disk.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_