How to set background color of outline labels in matplotlib?

I use the command:

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

to generate labels for my outlines, I would like them to be white (colors = 'white' works) with a red background, I can’t determine if it is possible to specify a background color for them?

+4
source share
2 answers

I'm late to the party for a few years, but this answer still appears on Google, so here is the solution I hacked inspired by @pelson's answer.

If you configured the contour graph as:

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

Then you can just refresh the background colors using

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

However, the bounding box ( bbox ) is quite large and often hides other functions unnecessarily. Therefore, it is better to update bbox immediately:

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

backgroundcolor text artist may be what you need ( http://matplotlib.org/users/text_props.html ). clabel provides text artists with 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 does not work, update your question using SSCCE and I will write some working code.

NTN

+3
source

All Articles