Re: Creating a Scatter Plot with Labeled Points
- To: mathgroup at smc.vnet.net
- Subject: [mg127171] Re: Creating a Scatter Plot with Labeled Points
- From: Helen Read <readhpr at gmail.com>
- Date: Thu, 5 Jul 2012 06:11:13 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jt0r72$17l$1@smc.vnet.net>
You'll probably get some slick solutions using pure functions, but I'm going to lead you through this step by step in a way that I think is easier for a beginner to understand. Lots of things to address. First off, it's a good idea to get in the habit of starting names with a lower case letter, so that you never accidentally conflict with the name of a built-in function or symbol. It also makes it easier to read your code -- you can see immediately which names you defined yourself, as opposed to names of built-in functions and symbols. The && you are using is the logical And symbol, which is why you are getting the ^. That is not how you join strings. Look up "string operations" in the Documentation Center. What you want is StringJoin, which is the very first item under String Operations, but you will need to use ToString to convert the coordinates of your points from expressions to strings. Look up ToString while you are at it. An even easier way to join a mix of strings and expressions, without having to convert everything ToString, is to use Row. Look that up in the Documentation Center too. Try it out on a single one of your points. point={0,120}; Row[{"(", point[[1]], ",", point[[2]], ")"}] Use StyleForm to format it. StyleForm[ Row[{"(", point[[1]], ",", point[[2]], ")"}], 14, Blue, Bold] Now make a Text graphic, and specify where to place the text in relation to the point. I'll name it "text". text=Text[StyleForm[Row[{"(", point[[1]], ",", point[[2]], ")"}], 14, Blue, Bold], {point[[1]] + 1, point[[2]] + 2}] Here's your list of points. I've renamed it "list". list = {{0, 120}, {10, 119}, {20, 118}, {30, 117}, {40, 116}, {50, 115}, {60, 114}, {70, 113}, {80, 112}}; Now plot the list and place the text we made above into the Epilog. I set an explicit PlotRange, which you can adjust however you like. Use PlotStyle to format the points in the ListPlot. ListPlot[list, PlotStyle -> {Blue, PointSize[Medium]}, Epilog -> text, PlotRange -> {{-10, 100}, {110, 125}}] Now, we want to label all the points at once. There is no need for a For loop to do this. We simply create a function to do what we want, and Map the function across the list of points. I'll use the variable "pt" to represent a generic point, and make a function that does exactly the same thing we did the example point. f[pt_] := Text[StyleForm[Row[{"(", pt[[1]], ",", pt[[2]], ")"}], 14, Blue, Bold], {pt[[1]] + 1, pt[[2]] + 1}] Check how it works on the example point. f[point] Now we Map this across the list of points, like this. Map[f,list] And put it in the Epilog. ListPlot[list, PlotStyle -> {Blue, PointSize[Medium]}, Epilog -> Map[f, list], PlotRange -> {{-10, 100}, {110, 125}}, AxesLabel -> {"x", "y"}] Helen Read University of Vermont On 7/4/2012 3:26 AM, Mike McCraith wrote: > Hello there. I'm trying to graph a Scatter Plot based on a given list of > data and have the coordinates appear near the point. > > I'm trying to save time and, instead of manually typing the text for the > coordinates, I'd like to be able to pull the coordinates from the list to > plot them point and list the text. Here's what I have so far: > > S11Exercise64b = {{0, 120}, {10, 119}, {20, 118}, {30, 117}, {40, 116}, > {50, 115}, {60, 114}, {70, 113}, {80, 112}}; > ListPlot[S11Exercise64b, AxesLabel -> {x, y}, AxesOrigin -> {0, 0}, > Epilog -> {Blue, PointSize[.02], Point[S11Exercise64b], > Text[Style[ > "(" && S11Exercise64b[[1, 1]] && "," && S11Exercise64b[[1, 2]] && ")", > FontSize -> 18], {.9*S11Exercise64b[[1, 1]] + 30, .9*S11Exercise64b[[1, 2]] > + .2}], > } > ] > > > Please note, for this example, I am the first point (0, 120). > > The line that begins with the "(" && S11Exercise64b...is my attempt to > extract the coordinates from the given list. Thus, next to the point, it > should read (0,120). However, I am getting ( ^ 0^, ^ 120 ^). Basically, > my lack of knowledge concerning strings is one of my issues. > > But, as you can see, once fixed, I'd have to create a Text[Style[ line for > every ordered pair that I have in the list. Is there a way to automate > this process? Again, a lack of know-how leads to now knowing how to do For > loops. > > Any help is always greatly appreciated. > > Thanks in advance! > > Mike >