Skip to content

Commit

Permalink
Allow passing of ax to Subarray.peek, do not always call tight_layout
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnoe committed Nov 17, 2023
1 parent 49ab82b commit 4ee3705
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
29 changes: 24 additions & 5 deletions ctapipe/instrument/subarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,20 +392,39 @@ def select_subarray(self, tel_ids, name=None) -> "SubarrayDescription":
)
return newsub

def peek(self):
def peek(self, ax=None):
"""
Draw a quick matplotlib plot of the array
Parameters
----------
ax : matplotlib.axes.Axes or None
If given, the subarray will be plotted into this ax.
"""
from matplotlib import pyplot as plt

from ctapipe.coordinates.ground_frames import EastingNorthingFrame
from ctapipe.visualization import ArrayDisplay

plt.figure(figsize=(8, 8))
ad = ArrayDisplay(subarray=self, frame=EastingNorthingFrame(), tel_scale=0.75)
if ax is None:
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(1, 1, 1)
else:
fig = ax.figure

ad = ArrayDisplay(
subarray=self, frame=EastingNorthingFrame(), tel_scale=0.75, axes=ax
)
ad.add_labels()
plt.title(self.name)
plt.tight_layout()
ax.set_title(self.name)

# This avoids a warning if users have e.g. "figure.constrained_layout"
# enabled through their matplotlib config. Only call tight layout here
# if the layout engine has not been set already
if fig.get_layout_engine() is None:
fig.tight_layout()

return ad

@lazyproperty
def telescope_types(self) -> Tuple[TelescopeDescription]:
Expand Down
11 changes: 11 additions & 0 deletions ctapipe/visualization/tests/test_mpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,14 @@ def test_overlay_coord(tmp_path, subarray_and_event_gamma_off_axis_500_gev):
)

fig.savefig(tmp_path / "coord_overlay.png", dpi=300)


@pytest.mark.parametrize("layout", (None, "constrained"))
def test_array_display_axes(tmp_path, subarray_prod5_paranal, layout):
"""Test passing axes to peek"""
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 8), layout=layout)

subarray_prod5_paranal.peek(ax=ax1)
subarray_prod5_paranal.peek(ax=ax2)

fig.savefig(tmp_path / "double_peek.png")
2 changes: 2 additions & 0 deletions docs/changes/2369.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow passing the matplotlib axes to the ``SubarrayDescription.peek`` function,
fix warnings in case of layout engine being already defined.

0 comments on commit 4ee3705

Please sign in to comment.