Re: Labelling contours on contour plots

*To*: mathgroup at smc.vnet.net*Subject*: [mg87657] Re: Labelling contours on contour plots*From*: "David Park" <djmpark at comcast.net>*Date*: Tue, 15 Apr 2008 05:50:26 -0400 (EDT)*References*: <200804140906.FAA05808@smc.vnet.net> <ftvd5q$d9l$1@smc.vnet.net>

The Presentations package gives some extra control for labeling contour lines, or any line in a graphic that is described by a constant function. Here is an example from your second function. I think it is better in these cases to use a specific set of regular contour values. The operative routine is: DrawLineLabels[ constant Function on line, positioning Function for labels, error allowed in matching contour values, list of values to be labeled]. The list of contours to be labeled does not have to be the full set of contours used in the contour plot. The positioning function might be 0.5& to label lines half way along their length, or RandomReal[{.3,.5}]& to label them randomly somewhere in the middle section. You don't have to use Normal on the contour graphics. If you wanted to double label the contours you could use two DrawLineLabels routines. The desirable contourlist used in this example would depend on the specific case generated by your function. Needs["Presentations`Master`"] f[x_, y_] = Evaluate[Sum[Sin[RandomReal[5, 2].{x, y}], {5}]] Sin[3.17456 x + 0.214736 y] + Sin[3.49989 x + 0.518615 y] + Sin[1.91662 x + 2.45835 y] + Sin[1.53246 x + 4.12794 y] + Sin[2.95345 x + 4.58416 y] Module[ {g, contourlist}, contourlist = Range[-4, 4, .25]; g = ContourDraw[f[x, y], {x, 0, 3}, {y, 0, 3}, Contours -> contourlist, ContourShading -> Automatic, ColorFunction -> ColorData["Pastel"], PlotRange -> {-5, 5}]; Draw2D[ {g, g // DrawLineLabels[f[#1, #2] &, .5 &, 0.01, Range[-4, 4, 1], DrawLLTextOptions -> {Background -> None, BaseStyle -> {FontSize -> 10, FontFamily -> "Helvetica"}}]}, Frame -> True, ImageSize -> 450] ] Only every 4th contour line is labeled. -- David Park djmpark at comcast.net http://home.comcast.net/~djmpark/ "Szabolcs Horv=E1t" <szhorvat at gmail.com> wrote in message news:ftvd5q$d9l$1 at 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] >