Skip to content

Commit

Permalink
Merge pull request gammapy#5594 from meeseeksmachine/auto-backport-of…
Browse files Browse the repository at this point in the history
…-pr-5585-on-v1.3.x

Backport PR gammapy#5585 on branch v1.3.x (Ax formatting of `FluxPoint.plot`)
  • Loading branch information
registerrier authored Nov 22, 2024
2 parents be735aa + 410d633 commit 7b4fdf0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
23 changes: 20 additions & 3 deletions gammapy/estimators/points/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,13 +667,30 @@ def plot(self, ax=None, sed_type=None, energy_power=0, time_format="iso", **kwar
flux = scale_plot_flux(flux=flux.to_unit(flux_unit), energy_power=energy_power)
if "time" in flux.geom.axes_names:
flux.geom.axes["time"].time_format = time_format
ax = flux.plot(ax=ax, **kwargs)
ax.set_ylabel(f"{sed_type} [{ax.yaxis.units.to_string(UNIT_STRING_FORMAT)}]")
ax.set_yscale("log")
ax = flux.plot(ax=ax, **kwargs)
else:
ax = flux.plot(ax=ax, **kwargs)
ax.set_xlabel(f"Energy [{ax.xaxis.units.to_string(UNIT_STRING_FORMAT)}]")
ax.set_xscale("log", nonpositive="clip")
self._plot_format_yax(ax=ax, energy_power=energy_power, sed_type=sed_type)

if len(flux.geom.axes) > 1:
ax.legend()
return ax

@staticmethod
def _plot_format_yax(ax, energy_power, sed_type):
if energy_power > 0:
ax.set_ylabel(
f"e{energy_power} * {sed_type} [{ax.yaxis.units.to_string(UNIT_STRING_FORMAT)}]"
)
else:
ax.set_ylabel(
f"{sed_type} [{ax.yaxis.units.to_string(UNIT_STRING_FORMAT)}]"
)

ax.set_yscale("log", nonpositive="clip")

def plot_ts_profiles(
self,
ax=None,
Expand Down
34 changes: 34 additions & 0 deletions gammapy/estimators/points/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,40 @@ def test_plot_likelihood_error(self, flux_points_likelihood):
flux_points_likelihood.plot_ts_profiles(ax=ax)


@requires_data()
def test_plot_format_yaxis():
path = "$GAMMAPY_DATA/tests/spectrum/flux_points/flux_points.fits"
fp = FluxPoints.read(path)

with mpl_plot_check():
ax = fp.plot(sed_type="e2dnde")
assert (
ax.yaxis.get_label().get_text()
== "e2dnde [$\\mathrm{erg\\,s^{-1}\\,cm^{-2}}$]"
)

with mpl_plot_check():
ax = fp.plot(sed_type="dnde", energy_power=2)
assert (
ax.yaxis.get_label().get_text()
== "e2 * dnde [$\\mathrm{TeV\\,s^{-1}\\,cm^{-2}}$]"
)

with mpl_plot_check():
ax = fp.plot(sed_type="dnde")
assert (
ax.yaxis.get_label().get_text()
== "dnde [$\\mathrm{TeV^{-1}\\,s^{-1}\\,cm^{-2}}$]"
)

with mpl_plot_check():
ax = fp.plot(sed_type="dnde", energy_power=2.7)
assert (
ax.yaxis.get_label().get_text()
== "e2.7 * dnde [$\\mathrm{TeV^{17/10}\\,s^{-1}\\,cm^{-2}}$]"
)


@requires_data()
def test_flux_points_single_bin_dnde():
path = make_path("$GAMMAPY_DATA/tests/spectrum/flux_points/diff_flux_points.fits")
Expand Down

0 comments on commit 7b4fdf0

Please sign in to comment.