patheffects_guide
Path effects guide
Defining paths that objects follow on a canvas.
Matplotlib’s patheffects module provides functionality to
apply a multiple draw stage to any Artist which can be rendered via aPath.
Artists which can have a path effect applied to them include Patch,Line2D, Collection and evenText. Each artist’s path effects can be controlled via theset_path_effects method (set_path_effects), which takes
an iterable of AbstractPathEffect instances.
The simplest path effect is the Normal effect, which simply
draws the artist without any effect:
1 | import matplotlib.pyplot as plt |

Whilst the plot doesn’t look any different to what you would expect without any path
effects, the drawing of the text now been changed to use the path effects
framework, opening up the possibilities for more interesting examples.
Adding a shadow
A far more interesting path effect than Normal is the
drop-shadow, which we can apply to any of our path based artists. The classesSimplePatchShadow andSimpleLineShadow do precisely this by drawing either a filled
patch or a line patch below the original artist:
1 | import matplotlib.patheffects as path_effects |

Notice the two approaches to setting the path effects in this example. The
first uses the with* classes to include the desired functionality automatically
followed with the “normal” effect, whereas the latter explicitly defines the two path
effects to draw.
Making an artist stand out
One nice way of making artists visually stand out is to draw an outline in a bold
color below the actual artist. The Stroke path effect
makes this a relatively simple task:
1 | fig = plt.figure(figsize=(7, 1)) |

It is important to note that this effect only works because we have drawn the text
path twice; once with a thick black line, and then once with the original text
path on top.
You may have noticed that the keywords to Stroke andSimplePatchShadow and SimpleLineShadow are not the usual Artist
keywords (such as facecolor and edgecolor etc.). This is because with these
path effects we are operating at lower level of matplotlib. In fact, the keywords
which are accepted are those for a matplotlib.backend_bases.GraphicsContextBase
instance, which have been designed for making it easy to create new backends - and not
for its user interface.
Greater control of the path effect artist
As already mentioned, some of the path effects operate at a lower level than most users
will be used to, meaning that setting keywords such as facecolor and edgecolor
raise an AttributeError. Luckily there is a generic PathPatchEffect path effect
which creates a PathPatch class with the original path.
The keywords to this effect are identical to those of PathPatch:
1 | fig = plt.figure(figsize=(8, 1)) |





