Add plot_spectrum_dataset_off_regions() helper method
@@ -13,7 +13,7 @@ from gammapy.utils.random import get_random_state
from gammapy.utils.scripts import make_path
from .core import CountsSpectrum, SpectrumEvaluator
-__all__ = ["SpectrumDatasetOnOff", "SpectrumDataset"]
+__all__ = ["SpectrumDatasetOnOff", "SpectrumDataset", "plot_spectrum_datasets_off_regions"]
class SpectrumDataset(Dataset):
@@ -913,7 +913,7 @@ class SpectrumDatasetOnOff(SpectrumDataset):
outdir = Path.cwd() if outdir is None else make_path(outdir)
outdir.mkdir(exist_ok=True, parents=True)
- phafile = f"pha_obs{self.name}.fits"
+ phafile = f"pha_{self.name}.fits"
bkgfile = phafile.replace("pha", "bkg")
arffile = phafile.replace("pha", "arf")
@@ -1115,3 +1115,31 @@ def _read_ogip_hdulist(hdulist, hdu1="SPECTRUM", hdu2="EBOUNDS", hdu3="GTI"):
is_bkg=False,
gti=gti,
)
+
+
+def plot_spectrum_datasets_off_regions(datasets, ax=None):
+ """Plot spectrum datasets of regions.
+
+ Parameters
+ ----------
+ datasets : list of `SpectrumDatasetOnOff`
+ List of spectrum on-off datasets
+ """
+ import matplotlib.pyplot as plt
+ import matplotlib.patches as mpatches
+
+ ax = plt.gca() or ax
+
+ color_cycle = plt.rcParams['axes.prop_cycle']
+ colors = color_cycle.by_key()['color']
+ handles = []
+
+ for color, dataset in zip(colors, datasets):
+ kwargs = {"edgecolor": color, "facecolor": "none"}
+ dataset.counts_off.plot_region(ax=ax, **kwargs)
+
+ # create proxy artist for the custom legend
+ handle = mpatches.Patch(label=dataset.name, **kwargs)
+ handles.append(handle)
+
+ plt.legend(handles=handles)
\ No newline at end of file
