Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set the background color of contour labels in matplotlib?

Tags:

matplotlib

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 ?

like image 484
tm8cc Avatar asked Apr 11 '13 19:04

tm8cc


People also ask

How do I change the contour color in Matplotlib?

The default color scheme of Matplotlib contour and filled contour plots can be modified. A general way to modify the color scheme is to call Matplotlib's plt. get_cmap() function that outputs a color map object.

How do you label contour maps in python?

If True , contour labels will be placed manually using mouse clicks. Click the first button near a contour to add a label, click the second button (or potentially both mouse buttons at once) to finish adding labels.

What is a contour Matplotlib?

MatPlotLib with Python Contour plots (sometimes called Level Plots) are a way to show a three-dimensional surface on a two-dimensional plane. It graphs two predictor variables X Y on the y-axis and a response variable Z as contours. These contours are sometimes called the z-slices or the iso-response values.


1 Answers

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]
like image 84
Greg Avatar answered Oct 14 '22 06:10

Greg