From bcc48bab3251239f33a5d0d39f65c686eeddc8ca Mon Sep 17 00:00:00 2001 From: "Adam Ginsburg (keflavich)" Date: Fri, 15 Jul 2022 09:57:03 +0200 Subject: [PATCH] WIP/proposal: plotting for compound artists --- regions/core/compound.py | 23 +++++++++++++++++++++-- regions/core/core.py | 14 ++++++++++---- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/regions/core/compound.py b/regions/core/compound.py index a26b7da3b..8c38cf964 100644 --- a/regions/core/compound.py +++ b/regions/core/compound.py @@ -123,6 +123,23 @@ def _make_annulus_path(patch_inner, patch_outer): return mpath.Path(verts, codes) + + def as_artists(self, origin=(0, 0), **kwargs): + """ + Return a list of patches for the composite region. + """ + # recursively retrieve all artist patches + if hasattr(self.region1, 'as_artists'): + r1p = self.region1.as_artists(origin=origin, **kwargs) + else: + r1p = [self.region1.as_artist(origin=origin, **kwargs)] + if hasattr(self.region2, 'as_artists'): + r2p = self.region2.as_artists(origin=origin, **kwargs) + else: + r2p = [self.region2.as_artist(origin=origin, **kwargs)] + return r1p + r2p + + def as_artist(self, origin=(0, 0), **kwargs): """ Return a matplotlib patch object for this region @@ -143,8 +160,10 @@ def as_artist(self, origin=(0, 0), **kwargs): patch : `~matplotlib.patches.PathPatch` A matplotlib patch object. """ - if (self.region1.center == self.region2.center - and self.operator is op.xor): + if (hasattr(self.region1, 'center') and + hasattr(self.region2, 'center') and + self.region1.center == self.region2.center and + self.operator is op.xor): import matplotlib.patches as mpatches diff --git a/regions/core/core.py b/regions/core/core.py index ea7e6017c..f996bb7c1 100644 --- a/regions/core/core.py +++ b/regions/core/core.py @@ -403,10 +403,16 @@ def plot(self, origin=(0, 0), ax=None, **kwargs): if ax is None: ax = plt.gca() - artist = self.as_artist(origin=origin, **kwargs) - ax.add_artist(artist) - - return artist + if hasattr(self, 'as_artists'): + artists = self.as_artists(origin=origin, **kwargs) + for artist in artists: + ax.add_artist(artist) + return artists + else: + artist = self.as_artist(origin=origin, **kwargs) + ax.add_artist(artist) + + return artist class SkyRegion(Region):