Share @ LinkedIn Facebook  datascience, datavisulisation, matplotlib
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)
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:

In [14]:
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