Improve Map m.plot_interactive a bit
@@ -528,7 +528,6 @@ class WcsNDMap(WcsMap):
ax.autoscale(enable=False)
return fig, ax, cbar
-
def plot_interactive(self, ax=None, fig=None, **kwargs):
"""
Plot ND array on matplotlib WCS axes with interactive widgets
@@ -542,28 +541,36 @@ class WcsNDMap(WcsMap):
Figure object.
**kwargs : dict
Keyword arguments passed to `~matplotlib.pyplot.imshow`.
+
+ Examples
+ --------
+
+ You can try this out e.g. using a Fermi-LAT diffuse model cube with an energy axis::
+
+ %matplotlib inline
+ from gammapy.maps import Map
+
+ m = Map.read("$GAMMAPY_EXTRA/datasets/vela_region/gll_iem_v05_rev1_cutout.fits")
+ m.plot_interactive(cmap='gnuplot2')
"""
import matplotlib.pyplot as plt
from astropy.visualization import simple_norm
from ipywidgets.widgets.interaction import interact, fixed
import ipywidgets as widgets
+ kwargs.setdefault('interpolation', 'nearest')
+ kwargs.setdefault('origin', 'lower')
+ kwargs.setdefault('cmap', 'afmhot')
@interact(
index=widgets.IntSlider(min=0, max=self.data.shape[0] - 1, step=1, value=1,
description=self.geom.axes_names[0] + ' slice'),
stretch=widgets.RadioButtons(options=['linear', 'sqrt', 'log'], value='sqrt',
description='Plot stretch'),
- mapND=fixed(self),
ax=fixed(ax),
fig=fixed(fig),
- kwargs=fixed(kwargs)
)
- def _plot_interactive(mapND, index, stretch='linear', ax=None, fig=None, **kwargs):
-
- from ipywidgets.widgets.interaction import interact, fixed
- import ipywidgets as widgets
-
+ def _plot_interactive(index, stretch, ax=None, fig=None):
if self.geom.is_image:
raise TypeError('Use .plot() for 2D Maps')
@@ -571,22 +578,21 @@ class WcsNDMap(WcsMap):
fig = plt.gcf()
if ax is None:
- ax = fig.add_subplot(1, 1, 1, projection=mapND.geom.wcs)
-
- axes = mapND.geom.get_axis_by_name(mapND.geom.axes_names[0])
+ ax = fig.add_subplot(1, 1, 1, projection=self.geom.wcs)
- data_2D = mapND.get_image_by_idx([index]).data
+ axes = self.geom.get_axis_by_name(self.geom.axes_names[0])
- kwargs.setdefault('interpolation', 'nearest')
- kwargs.setdefault('origin', 'lower')
- kwargs.setdefault('cmap', 'afmhot')
- norm = simple_norm(data_2D[np.isfinite(data_2D)], stretch)
- kwargs.setdefault('norm', norm)
+ data = self.get_image_by_idx([index]).data
+ norm = simple_norm(data[np.isfinite(data)], stretch)
- caxes = ax.imshow(data_2D, **kwargs['kwargs'])
+ caxes = ax.imshow(data, norm=norm, **kwargs)
fig.colorbar(caxes, ax=ax)
- ax.set_title('{:.2f}-{:.2f} {} '.format(axes.edges[index],
- axes.edges[index + 1], mapND.geom.axes[0].unit.name))
+ ax.set_title(
+ '{:.2f}-{:.2f} {} '.format(
+ axes.edges[index], axes.edges[index + 1],
+ self.geom.axes[0].unit.name,
+ )
+ )
try:
ax.coords['glon'].set_axislabel('Galactic Longitude')
