11"""
2- Defines classes for path effects. The path effects are supported in
3- :class:`~matplotlib.text.Text`, :class:`~matplotlib.lines.Line2D`
4- and :class:`~matplotlib.patches.Patch`.
2+ Defines classes for path effects. The path effects are supported in `~.Text`,
3+ `~.Line2D` and `~.Patch`.
4+
5+ .. seealso::
6+ :doc:`/tutorials/advanced/patheffects_guide`
57"""
68
79from matplotlib .backend_bases import RendererBase
@@ -159,7 +161,35 @@ class Normal(AbstractPathEffect):
159161 The Normal PathEffect's sole purpose is to draw the original artist with
160162 no special path effect.
161163 """
162- pass
164+
165+
166+ def _subclass_with_normal (effect_class ):
167+ """
168+ Create a PathEffect class combining *effect_class* and a normal draw.
169+ """
170+
171+ class withEffect (effect_class ):
172+ def draw_path (self , renderer , gc , tpath , affine , rgbFace ):
173+ super ().draw_path (renderer , gc , tpath , affine , rgbFace )
174+ renderer .draw_path (gc , tpath , affine , rgbFace )
175+
176+ withEffect .__name__ = f"with{ effect_class .__name__ } "
177+ withEffect .__doc__ = f"""
178+ A shortcut PathEffect for applying `.{ effect_class .__name__ } ` and then
179+ drawing the original Artist.
180+
181+ With this class you can use ::
182+
183+ artist.set_path_effects([path_effects.with{ effect_class .__name__ } ()])
184+
185+ as a shortcut for ::
186+
187+ artist.set_path_effects([path_effects.{ effect_class .__name__ } (),
188+ path_effects.Normal()])
189+ """
190+ # Docstring inheritance doesn't work for locally-defined subclasses.
191+ withEffect .draw_path .__doc__ = effect_class .draw_path .__doc__
192+ return withEffect
163193
164194
165195class Stroke (AbstractPathEffect ):
@@ -184,15 +214,7 @@ def draw_path(self, renderer, gc, tpath, affine, rgbFace):
184214 gc0 .restore ()
185215
186216
187- class withStroke (Stroke ):
188- """
189- Adds a simple :class:`Stroke` and then draws the
190- original Artist to avoid needing to call :class:`Normal`.
191- """
192-
193- def draw_path (self , renderer , gc , tpath , affine , rgbFace ):
194- Stroke .draw_path (self , renderer , gc , tpath , affine , rgbFace )
195- renderer .draw_path (gc , tpath , affine , rgbFace )
217+ withStroke = _subclass_with_normal (effect_class = Stroke )
196218
197219
198220class SimplePatchShadow (AbstractPathEffect ):
@@ -261,15 +283,7 @@ def draw_path(self, renderer, gc, tpath, affine, rgbFace):
261283 gc0 .restore ()
262284
263285
264- class withSimplePatchShadow (SimplePatchShadow ):
265- """
266- Adds a simple :class:`SimplePatchShadow` and then draws the
267- original Artist to avoid needing to call :class:`Normal`.
268- """
269-
270- def draw_path (self , renderer , gc , tpath , affine , rgbFace ):
271- SimplePatchShadow .draw_path (self , renderer , gc , tpath , affine , rgbFace )
272- renderer .draw_path (gc , tpath , affine , rgbFace )
286+ withSimplePatchShadow = _subclass_with_normal (effect_class = SimplePatchShadow )
273287
274288
275289class SimpleLineShadow (AbstractPathEffect ):
0 commit comments