Add support for time axis plotting to RegionNDMap.plot
@@ -9,7 +9,7 @@ from gammapy.utils.interpolation import ScaledRegularGridInterpolator
from gammapy.utils.scripts import make_path
from .core import Map
from .geom import pix_tuple_to_idx
-from .axes import MapAxes, MapAxis
+from .axes import MapAxes, MapAxis, TimeMapAxis
from .region import RegionGeom
from .utils import INVALID_INDEX
@@ -50,13 +50,15 @@ class RegionNDMap(Map):
self.meta = meta
self.unit = u.Unit(unit)
- def plot(self, ax=None, **kwargs):
+ def plot(self, ax=None, axis_name="energy", **kwargs):
"""Plot the data contained in region map along the non-spatial axis.
Parameters
----------
ax : `~matplotlib.pyplot.Axis`
Axis used for plotting
+ axis_name : str
+ Which axis to plot on the x axis. Extra axes will be plotted as additional lines.
**kwargs : dict
Keyword arguments passed to `~matplotlib.pyplot.errorbar`
@@ -66,6 +68,7 @@ class RegionNDMap(Map):
Axis used for plotting
"""
import matplotlib.pyplot as plt
+ from matplotlib.dates import DateFormatter
ax = ax or plt.gca()
@@ -74,14 +77,25 @@ class RegionNDMap(Map):
"Use `.plot_interactive()` if more the one extra axis is present."
)
- axis = self.geom.axes[0]
+ if axis_name is None:
+ axis_name = 0
+
+ axis = self.geom.axes[axis_name]
kwargs.setdefault("fmt", ".")
kwargs.setdefault("capsize", 2)
kwargs.setdefault("lw", 1)
with quantity_support():
- ax.errorbar(axis.center, self.quantity.squeeze(), xerr=axis.as_xerr, **kwargs)
+ if isinstance(axis, TimeMapAxis):
+ if axis.time_format == "iso":
+ center = axis.time_mid.datetime
+ else:
+ center = axis.time_mid.mjd * u.day
+ else:
+ center = axis.center
+
+ ax.errorbar(center, self.quantity.squeeze(), xerr=axis.as_xerr, **kwargs)
if axis.interp == "log":
ax.set_xscale("log")
@@ -94,8 +108,18 @@ class RegionNDMap(Map):
if axis.interp == "log":
ax.set_yscale("log")
+ if isinstance(axis, TimeMapAxis) and axis.time_format == "iso":
+ ax.xaxis.set_major_formatter(DateFormatter("%Y-%m-%d %H:%M:%S"))
+ plt.setp(
+ ax.xaxis.get_majorticklabels(),
+ rotation=30,
+ ha="right",
+ rotation_mode="anchor",
+ )
+
return ax
+
def plot_hist(self, ax=None, **kwargs):
"""Plot as histogram.
