13
$\begingroup$
ContourPlot[x^2 + 5 y^2, {x, -5, 5}, {y, -2, 2}, 
  Contours -> {2, 4, 6, 8, 10, 12, 14, 16, 20}, 
  ContourLabels -> True, 
  AspectRatio -> Automatic, 
  ContourShading -> None]

gives result

enter image description here

But I want result like this

enter image description here

So I hope this code will work

ContourPlot[x^2 + 5 y^2, {x, -5, 5}, {y, -2, 2}, 
  Contours -> {2, 4, 6, 8, 10, 12, 14, 16, 20}, 
  ContourLabels -> (Text[#3, {#1, 0}, Background -> White] &), 
  AspectRatio -> Automatic, 
  ContourShading -> None]

But it didn't, it gives result

enter image description here

So how to control the position of ContourLabels?

$\endgroup$
  • 1
    $\begingroup$ None of the workarounds offered in the answers (so far) explains why ContourPlot is giving the wrong coordinate for 14. What am I missing? $\endgroup$ – rhermans Sep 11 '14 at 9:20
  • 2
    $\begingroup$ @rhermans - Unlike for the other labels, the default location for the label 14 is far from the x=0 (semi-major) axis so that when its y coordinate is just changed to zero without also correcting the x coordinate it ends up in the wrong position. The problem is not with ContourPlot but rather with the manual positioning of that label. $\endgroup$ – Bob Hanlon Sep 11 '14 at 12:32
  • $\begingroup$ @Bob_Hanlon, thanks for the explanation. $\endgroup$ – rhermans Sep 11 '14 at 12:34
10
$\begingroup$

This can done with Epilog.

f[x_, y_] := x^2 + 5 y^2;
contours = {2, 4, 6, 8, 10, 12, 14, 16, 20};
lblXY = {#, 0} & /@ (Solve[f[x, 0] == #, x][[2, 1, 2]] & /@ contours // N);

ContourPlot[f[x, y], {x, -5, 5}, {y, -2, 2},
  Contours -> contours,
  AspectRatio -> Automatic,
  ContourShading -> None,
  Epilog -> {Thread[Text[contours, lblXY, Background -> White]]}]

contours

$\endgroup$
8
$\begingroup$

Kind of a hack but this is what I would do.

f[x_, y_] := x^2 + 5 y^2;
c = {2, 4, 6, 8, 10, 12, 14, 16, 20};
labelPos = Solve[f[x, 0] == #, x][[2, 1, 2]] & /@ c;

Show[
 ContourPlot[f[x, y], {x, -5, 5}, {y, -2, 2}, Contours -> c, 
  AspectRatio -> Automatic, ContourShading -> None],
 Graphics[Text[#[[2]], {#[[1]], 0}, {0, 0}, Background -> White]] & /@
   Transpose[{labelPos, c}]
 ]

enter image description here

$\endgroup$
7
$\begingroup$

Yes another workaround

Show@{ContourPlot[x^2 + 5 y^2, {x, -5, 5}, {y, -2, 2}, 
   Contours -> Range[2, 20, 2], AspectRatio -> Automatic, 
   ContourShading -> None, ImageSize -> 640],
  ContourPlot[x^2 + 5 y^2, {x, 0, 5}, {y, -0.0001, 0.0001}, 
   Contours -> Range[2, 20, 2], 
   ContourLabels -> (Text[#3, {#1, #2}, Background -> White] &), 
   ContourShading -> None]}

enter image description here

I decrease the possible region for labels by the second ContourPlot.

$\endgroup$
4
$\begingroup$
f = x^2 + 5 y^2;
ContourPlot[f, {x, -5, 5}, {y, -2, 2}, 
 Contours -> {2, 4, 6, 8, 10, 12, 14, 16, 20},
 ContourLabels -> (Text[#3, {Max[x /. Solve[f == #3 /. y -> 0, x]], 0},
   Background -> White] &),
 AspectRatio -> Automatic, ContourShading -> None]

enter image description here

Use

ContourLabels -> (Text[#3,  Max[x /. Solve[(f /. y -> x/3) == #3, x]] {1, 1/3}, 
  Background -> White] &)

to get

enter image description here

Update: you can also post-process the ContourPlot output to modify the location of the labels:

cp = ContourPlot[x^2 + 5 y^2, {x, -5, 5}, {y, -2, 2}, 
  Contours -> {2, 4, 6, 8, 10, 12, 14, 16, 20}, PlotPoints -> 200, 
  ContourLabels -> Automatic, AspectRatio -> Automatic, 
  ContourShading -> None]; 

Normal[cp] /. Tooltip[a_, b_] :> {a, 
   Text[Style[b, 16], 
    Nearest[{1, 2 GoldenRatio} # & /@ a[[-1, 1]], {.5, 0}, 1][[1]], 
    Background -> White]}

enter image description here

$\endgroup$
-1
$\begingroup$

I liked the answer, where the labels were printed on the diagonal, but such placing can often lead to overlapping of some labels, so, I modified it to add random shifts: (the example is in my context)

labelfunction := (Shift = RandomReal[{-30, 30}]; 
Text[#3, {e, 100 - e/3 + Shift} /.FindRoot[(ff - #3, {e,100}]]) & ;
ContourPlot[... , ContourLabels -> labelfunction]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.