Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Combine withEffect PathEffect definitions. #15515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions lib/matplotlib/patheffects.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""
Defines classes for path effects. The path effects are supported in
:class:`~matplotlib.text.Text`, :class:`~matplotlib.lines.Line2D`
and :class:`~matplotlib.patches.Patch`.
Defines classes for path effects. The path effects are supported in `~.Text`,
`~.Line2D` and `~.Patch`.

.. seealso::
:doc:`/tutorials/advanced/patheffects_guide`
"""

from matplotlib.backend_bases import RendererBase
Expand Down Expand Up @@ -159,7 +161,35 @@ class Normal(AbstractPathEffect):
The Normal PathEffect's sole purpose is to draw the original artist with
no special path effect.
"""
pass


def _subclass_with_normal(effect_class):
"""
Create a PathEffect class combining *effect_class* and a normal draw.
"""

class withEffect(effect_class):
def draw_path(self, renderer, gc, tpath, affine, rgbFace):
super().draw_path(renderer, gc, tpath, affine, rgbFace)
renderer.draw_path(gc, tpath, affine, rgbFace)

withEffect.__name__ = f"with{effect_class.__name__}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So... if you have to do this big song and dance for the docs, is this change really worth it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more worth it if we keep adding new patheffects and repeat this code pattern again and again, e.g. #15458.

withEffect.__doc__ = f"""
A shortcut PathEffect for applying `.{effect_class.__name__}` and then
drawing the original Artist.

With this class you can use ::

artist.set_path_effects([path_effects.with{effect_class.__name__}()])

as a shortcut for ::

artist.set_path_effects([path_effects.{effect_class.__name__}(),
path_effects.Normal()])
"""
# Docstring inheritance doesn't work for locally-defined subclasses.
withEffect.draw_path.__doc__ = effect_class.draw_path.__doc__
return withEffect


class Stroke(AbstractPathEffect):
Expand All @@ -184,15 +214,7 @@ def draw_path(self, renderer, gc, tpath, affine, rgbFace):
gc0.restore()


class withStroke(Stroke):
"""
Adds a simple :class:`Stroke` and then draws the
original Artist to avoid needing to call :class:`Normal`.
"""

def draw_path(self, renderer, gc, tpath, affine, rgbFace):
Stroke.draw_path(self, renderer, gc, tpath, affine, rgbFace)
renderer.draw_path(gc, tpath, affine, rgbFace)
withStroke = _subclass_with_normal(effect_class=Stroke)


class SimplePatchShadow(AbstractPathEffect):
Expand Down Expand Up @@ -261,15 +283,7 @@ def draw_path(self, renderer, gc, tpath, affine, rgbFace):
gc0.restore()


class withSimplePatchShadow(SimplePatchShadow):
"""
Adds a simple :class:`SimplePatchShadow` and then draws the
original Artist to avoid needing to call :class:`Normal`.
"""

def draw_path(self, renderer, gc, tpath, affine, rgbFace):
SimplePatchShadow.draw_path(self, renderer, gc, tpath, affine, rgbFace)
renderer.draw_path(gc, tpath, affine, rgbFace)
withSimplePatchShadow = _subclass_with_normal(effect_class=SimplePatchShadow)


class SimpleLineShadow(AbstractPathEffect):
Expand Down