14
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?

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
matheorem
  • 17,132
  • 8
  • 45
  • 115
  • 1
    None of the workarounds offered in the answers (so far) explains why ContourPlot is giving the wrong coordinate for 14. What am I missing? – rhermans Sep 11 '14 at 09:20
  • 2
    @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. – Bob Hanlon Sep 11 '14 at 12:32
  • @Bob_Hanlon, thanks for the explanation. – rhermans Sep 11 '14 at 12:34

5 Answers5

10

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

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
8

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

paw
  • 5,650
  • 23
  • 31
7

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.

ybeltukov
  • 43,673
  • 5
  • 108
  • 212
4
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

kglr
  • 394,356
  • 18
  • 477
  • 896
-1

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]