MathGroup Archive 2008

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

Search the Archive

Re: Labelling contours on contour plots

  • To: mathgroup at smc.vnet.net
  • Subject: [mg87626] Re: Labelling contours on contour plots
  • From: "Szabolcs HorvÃt" <szhorvat at gmail.com>
  • Date: Mon, 14 Apr 2008 06:54:21 -0400 (EDT)
  • References: <200804140906.FAA05808@smc.vnet.net>

I would like to create a contour plot with traditional, "map-style"
labels.  Here are some examples of such plots:

http://upload.wikimedia.org/wikipedia/en/f/fa/Cntr-map-1.jpg
http://www.mathworks.com/products/demos/shipping/map/mapexmap_15.png
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/contour1.gif

I attached what I have so far, but the approach I used has several problems:

1. It doesn't work well if ContourShading is turned on. (One problem is
the background of the text, another problem is that Normal[], which is
used to expand GraphicsComplex objects so that p1 and p2 will be real
coordinate-pairs, makes the shading ugly.)

2. I do not know how to position the labels in a way so that they will
be close enough to each other to be readable, but still not overlap.
Note how on the gr1 example some labels are drawn at one end of the
contour lines, some at the other end.

3. It would be nice to be able to drop labels from regions of high
gradients (to avoid overlapping/crowding).


How could the labels be created in a better way, to avoid these problems?


(* example graphics *)

gr1 = Normal@
   ContourPlot[Sin[x] Sin[y], {x, 0, 5}, {y, 0, 5}, PlotPoints -> 30,
    ContourShading -> None]

gr2 = Normal@
   ContourPlot[
    Evaluate[Sum[Sin[RandomReal[5, 2].{x, y}], {5}]],
    {x, 0, 3}, {y, 0, 3}, ContourShading -> None, Contours -> 10]

(* create labels *)

(* This function collects the labels from the Tooltip objects, and
 positions them between the 21st and 22nd points of the contour line. *)

makeLabels[gr_] :=
  gr /.
    Tooltip[line : Line[{Longest@Repeated[_, 20], p1_, p2_, ___}],
      label_] :> {line, {Opacity[1],
      Rotate[Text[label, (p1 + p2)/2, Background -> White],
       Mod[ArcTan @@ (p2 - p1) + Pi/2, Pi] - Pi/2]}}

makeLabels[gr1]
makeLabels[gr2]


  • Prev by Date: mx files
  • Next by Date: Re: Dynamically generating Manipulate
  • Previous by thread: Re: mx files
  • Next by thread: Re: Labelling contours on contour plots