.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/c_profiles/plot_comparing_density_profiles.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_c_profiles_plot_comparing_density_profiles.py: ===================================== Comparing Common Density Profiles ===================================== This example compares several widely used spherically symmetric density profiles in astrophysics: - :class:`~profiles.density.NFWDensityProfile` - :class:`~profiles.density.HernquistDensityProfile` - :class:`~profiles.density.EinastoDensityProfile` - :class:`~profiles.density.TNFWDensityProfile` - :class:`~profiles.density.SNFWDensityProfile` We plot the radial dependence of density on log–log axes to highlight differences in the central cusp/core and outer slope behavior. In doing so, we see how users can easily instantiate and evaluate these profiles using the `pisces` package. For more detailed documentation on profiles, see :ref:`profiles_overview`. .. GENERATED FROM PYTHON SOURCE LINES 20-23 .. code-block:: Python import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 24-30 Setup ----- We'll need to import the relevant profile classes as well as standard scientific Python packages. We'll use `unyt` for unit handling, `numpy` for numerical operations, and `matplotlib` for plotting. We can import all of the relevant profiles from the :mod:`~profiles.density` module. .. GENERATED FROM PYTHON SOURCE LINES 30-41 .. code-block:: Python import numpy as np import unyt from pisces.profiles.density import ( EinastoDensityProfile, HernquistDensityProfile, NFWDensityProfile, SNFWDensityProfile, TNFWDensityProfile, ) .. GENERATED FROM PYTHON SOURCE LINES 42-47 Building Profiles ----------------- To create instances of a particular radial profile, we simply need to specify the relevant parameters. Each profile class has a different set of parameters, but they all share a common interface for evaluation. .. GENERATED FROM PYTHON SOURCE LINES 47-63 .. code-block:: Python # Define a set of characteristic quantities that we'll use as # parameters for the different profiles. rho_0 = unyt.unyt_quantity(1.0, "Msun/kpc**3") # Characteristic density r_s = unyt.unyt_quantity(10.0, "kpc") # Scale radius r_t = unyt.unyt_quantity(100.0, "kpc") # Tidal radius for TNFW M = unyt.unyt_quantity(1e4, "Msun") # Total mass for SNFW profiles = { "NFW": NFWDensityProfile(rho_0=rho_0, r_s=r_s), "Hernquist": HernquistDensityProfile(rho_0=rho_0, r_s=r_s), "Einasto": EinastoDensityProfile(rho_0=rho_0, r_s=r_s, alpha=0.6), "TNFW": TNFWDensityProfile(rho_0=rho_0, r_s=r_s, r_t=r_t), "SNFW": SNFWDensityProfile(M=M, a=r_s), } .. GENERATED FROM PYTHON SOURCE LINES 64-69 Plot Density Profiles --------------------- We'll now evaluate and plot the density profiles over a range of radii. We'll use logarithmic axes to clearly show differences in the inner and outer slopes. .. GENERATED FROM PYTHON SOURCE LINES 69-83 .. code-block:: Python r = unyt.unyt_array(np.logspace(0, 3, 500), "kpc") # Radii from 1 kpc to 1000 kpc plt.figure(figsize=(8, 6)) for name, profile in profiles.items(): rho = profile(r).to("Msun/kpc**3") plt.loglog(r.to("kpc"), rho, label=name) plt.xlabel("Radius [kpc]") plt.ylabel(r"Density [$M_\odot \, {\rm kpc^{-3}}$]") plt.title("Comparison of Common Density Profiles") plt.legend() plt.grid(True, which="both", ls="--", alpha=0.5) plt.show() .. image-sg:: /auto_examples/c_profiles/images/sphx_glr_plot_comparing_density_profiles_001.png :alt: Comparison of Common Density Profiles :srcset: /auto_examples/c_profiles/images/sphx_glr_plot_comparing_density_profiles_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.312 seconds) .. _sphx_glr_download_auto_examples_c_profiles_plot_comparing_density_profiles.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_comparing_density_profiles.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_comparing_density_profiles.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_comparing_density_profiles.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_