How to set the background color of contour labels
colors, contour, label, matplotlib, python
Solution
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]
Problem
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 ?