MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Contour labeling

  • To: mathgroup at
  • Subject: [mg24350] RE: [mg24288] Contour labeling
  • From: "David Park" <djmp at>
  • Date: Sun, 9 Jul 2000 04:52:54 -0400 (EDT)
  • Sender: owner-wri-mathgroup at

> -----Original Message-----
> From: Tom Aldenberg [mailto:Tom.Aldenberg at]
To: mathgroup at
> Dear All,
> How can one label contour lines with their function values? For
> example, when
> using the following command:
> ContourPlot[Sin[x y], {x, -1, 1}, {y, -1, 1}, Contours->{-.5, .5},
> ContourShading->False]
> one wants to indicate, which contour refers to -.5,  and which one to .5.
> Best regards,
> Peter Janssen


The Tom Wickham-Jones ExtendGraphics routines, available from MathSource at
the Wolfram site, have a package ExtendGraphics`LabelContour` which will
label contours. However, when I tried this in Version 4 it did not work
because it uses an obsolete graphics option. I don't know if his package has
been updated.

Considering that problem, and also considering that it might not just be
contour lines that we wish to label, I wrote a new routine which I hope will
be useful.

Options[DrawLineLabels] = {DrawLLFormat -> (NumberForm[#1, 2] & ),
DrawLLTextOptions -> {}};

DrawLineLabels::usage = "DrawLineLabels[function, position, opts][expr] will
label all the \
lines in expr. function is a pure function of two variables which is applied
to the choosen \
point to generate the value of the label. position is a pure function that
should generate a \
number between 0 and 1. It specifies the fractional number of plot points
along the line for \
the label placement. position = 0.5& will put the labels at roughly the
midpoint of each line. \
position = Random[]& will put the labels at random positions. The option
DrawLLFormat can be \
used to set a format specification for the labels. The default is
(NumberForm[#,2]&). The \
option DrawLLTextOptions can be used to add options to the Text statements
that produce the \

DrawLineLabels[function_, fracposition_, opts___][expr_] :=
  Module[{lines, format, textoptions},
   format := DrawLLFormat /. {opts} /. Options[DrawLineLabels];
    textoptions = DrawLLTextOptions /. {opts} /. Options[DrawLineLabels];
    lines = Flatten[Cases[expr, Line[_], Infinity]];
    (Module[{pos, val, point, len}, len = Length[#1[[1]]];
        pos = Min[Max[Floor[fracposition[]*len], 1], len]; point =
        val = function @@ point; Text[format[val], point, Sequence @@
textoptions]] & ) /@

Here is an example of its use on a contour plot. I used the regular number
of contours, and also color shading.

p1 = ContourPlot[Sin[x*y], {x, -1, 1}, {y, -1, 1}, ColorFunction -> Hue,
DisplayFunction -> Identity];

Show[p1, Graphics[DrawLineLabels[Sin[#1*#2] & , 0.5 & , DrawLLTextOptions ->
       {Background -> GrayLevel[1], TextStyle -> {FontSize ->
   ImageSize -> 500, DisplayFunction -> $DisplayFunction];

Here is an example of its use in labeling multiple curves from a single Plot

f[x_, c_] := 2c x + c

This gives us an expression for c, if we know a point {x, y} on a curve. We
will use it to obtain a value for labeling each line.

Solve[y == f[x, c], c]
{{c -> y/(1 + 2*x)}}

p2 = Plot[Evaluate[Table[f[x, c], {c, -2, 3}]], {x, -2, 2},
      DisplayFunction -> Identity];

Show[p2, Graphics[DrawLineLabels[#2/(1 + 2*#1) & , 0.8 & ,
DrawLLTextOptions ->
       {Background -> GrayLevel[1], TextStyle -> {FontSize ->
   ImageSize -> 500, DisplayFunction -> $DisplayFunction];

I am planning to add this routine to my DrawingPaper package in the near
future. It is somewhat easier to use there.

David Park
djmp at

  • Prev by Date: Re: Divisors
  • Next by Date: Re: A strange bug in Solve
  • Previous by thread: Re: Contour labeling
  • Next by thread: combining graphics