5

I'm using the command :

axins.clabel(c, levls, fontsize=4, fmt='%4.2f', colors= 'white')

to generate labels for my contours, I'd like them to be white (colors='white' works) with a red background, I can't find whether it's possible or not to specify a background color for them ?

2 Answers 2

9

I am several years late to the part party, but this answer is still coming up on Google so here is the solution I hacked inspired by @pelson's answer.

If you set up the contour plot as:

CS = ax.contour(X, Y, Z)
clabels = ax.clabel(CS)

Then you can simply update the background colours using

[txt.set_backgroundcolor('white') for txt in clabels]

However the bounding box (bbox) is quite large and often obscures other features unnecessarily. So it is better to update the bbox directly:

[txt.set_bbox(dict(facecolor='white', edgecolor='none', pad=0)) for txt in clabels]
3

The backgroundcolor of a text artist might be what you need (http://matplotlib.org/users/text_props.html). clabel exposes the text artists through the labelTexts attribute (appears to be undocumented).

Something like (untested):

clabels = ax.clabel(c, levls, color='white', ...)
[txt.set_backgroundcolor('red') for txt in clabels.labelTexts].

If this doesn't work, please update your question with a SSCCE and I'll post some working code.

HTH,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.