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]