Updated On : Dec-15,2019 Tags datascience, datavisulis‚Ķ
matplotlib - Path Effect

Matplotlib - Path Effect

Defining paths that objects follow on a canvas.

Matplotlib's Path Effects Module provides functionality to apply a multiple draw stage to any Artist which can be rendered via a Path.

Artists who can have a path effect applied to them include Patch, Line2D, Collection, and even Text. Each artist's path effects can be controlled via the set_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:

In [25]:
import matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects

text = fig.text(0.5, 0.5, 'Hello path effects world!\nThis is the normal '
                          'path effect.\nPretty dull, huh?',
                ha='center', va='center', size=20)

Whilst the plot doesn't look any different from 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 classes SimplePatchShadow and SimpleLineShadow do precisely this by drawing either a filled patch or a line patch below the original artist:

In [14]:
import matplotlib.patheffects as path_effects

text = plt.text(0.5, 0.5, 'Hello path effects world!',

plt.plot([0, 3, 2, 5], linewidth=5, color='blue',

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:

Dolly Solanki  Dolly Solanki

  Support Us

Thank You for visiting our website. If you like our work, please support us so that we can keep on creating new tutorials/blogs on interesting topics (like AI, ML, Data Science, Python, Digital Marketing, SEO, etc.) that can help people learn new things faster. You can support us by clicking on the Coffee button at the bottom right corner. We would appreciate even if you can give a thumbs-up to our article in the comments section below.

 Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to let us know in the comments section below (Guest Comments are allowed). We appreciate and value your feedbacks.