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]
source share