Add border around text with matplotlib

Sometimes text labels on plots can be hidden if there is too little contrast with a background color.  You can add borders to text objects (and probably most other patches) with “PathEffects“, see also http://matplotlib.org/examples/pylab_examples/patheffect_demo.html.

Below is an example:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patheffects as PathEffects

plt.imshow(np.zeros((5,5), cmap=plt.gray())
txt = plt.text(2,2,'This is a test', size=11, color='black')
txt.set_path_effects([PathEffects.withStroke(linewidth=5, foreground='w')])
plt.draw()

textborder

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s