# Customizing contour labels in ContourPlot

I have a contour plot. I want to make some changes in the contour labels. I want them to place along each contour in white color. Here is my code

ClearAll["Global*"];
f[x_, y_] := x + y^-1;
p = ContourPlot[
f[x, y], {x, -5, 5}, {y, -5, 5},
Contours -> 10,
ContourLabels -> True,
LabelStyle -> {Directive[Bold, Medium], White,
RotateLabel -> True},
ImageSize -> Large
]


The output of this code is but I want something like this How can I figure it out? Many thanks.

You can use a pure function with a Text object as the ContourLabels option setting. The fourth argument of Text specifies the direction, which can be computed with:

f[x_, y_] := x + y^-1
t[x_, y_] = Grad[f[x, y], {x, y}] . {{0, -1}, {1, 0}};


Then:

ContourPlot[
f[x, y],
{x,-5,5},
{y,-5,5},
Contours->10,
ContourLabels->Function[{x,y,z},Text[z,{x,y},{0,0},t[x,y], BaseStyle->FontColor->White]]
] • To make the contour labels more readable (i.e., no upside down contour labels), I've added in the 'function t2[z_] := If[z[] < 0 && z[] < 0, -z, If[z[] < 0, {-z[], z[]}, z]] and replaced t[x, y] with t2[t[x, y]]. Is there a better way? (Is this a separate question?)
– JimB
Dec 6, 2019 at 6:57
• After looking at this a bit more just using Sign[t[x, y][]] t[x, y] in place of t[x, y] seems to do the trick.
– JimB
Dec 7, 2019 at 16:14