Updated On : Dec-15,2019 Time Investment : ~11 mins

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:

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)
text.set_path_effects([path_effects.Normal()])
plt.show()

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:

import matplotlib.patheffects as path_effects

text = plt.text(0.5, 0.5, 'Hello path effects world!',
                path_effects=[path_effects.withSimplePatchShadow()])

plt.plot([0, 3, 2, 5], linewidth=5, color='blue',
         path_effects=[path_effects.SimpleLineShadow(),
                       path_effects.Normal()])
plt.show()

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

Share Views Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

When going through coding examples, it's quite common to have doubts and errors.

If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.

You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.

Share Views 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 contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.