Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating a Scatter Plot with Labeled Points

134 views
Skip to first unread message

Mike McCraith

unread,
Jul 4, 2012, 3:26:26 AM7/4/12
to

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

Helen Read

unread,
Jul 5, 2012, 6:12:19 AM7/5/12
to
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

djmpark

unread,
Jul 5, 2012, 6:13:51 AM7/5/12
to
In this case at least, throw ListPlot into the ashcan. Instead use Graphics
and just Map the desired point display onto the coordinate list.

S11Exercise64b = {{0, 120}, {10, 119}, {20, 118}, {30, 117}, {40,
116}, {50, 115}, {60, 114}, {70, 113}, {80, 112}};

If the plot is going to be viewed in Mathematica then I think the best
solution might be to put a Tooltip on each point.

Graphics[
{AbsolutePointSize[4],
Tooltip[Point[#], #] & /@ S11Exercise64b},
AspectRatio -> 1/2,
PlotRange -> {{-5, 85}, {100, 130}},
PlotRangePadding -> Automatic,
Axes -> True, AxesLabel -> {x, y}, AxesOrigin -> {-5, 100}]

If you want to "print" the coordinates next to the point you could use:

Graphics[
{AbsolutePointSize[4],
{Point[#], Text[#, #, {0, -2}]} & /@ S11Exercise64b},
AspectRatio -> 1/2,
PlotRange -> {{-5, 85}, {100, 130}},
PlotRangePadding -> Automatic,
Axes -> True, AxesLabel -> {x, y}, AxesOrigin -> {-5, 100},
ImageSize -> 600]

But there I had to increase the ImageSize to keep the labels from cluttering
each other. As an alternative we could print the coordinates vertically.

Graphics[
{AbsolutePointSize[4],
{Point[#], Text[#, #, {-1.2, 0}, {0, 1}]} & /@ S11Exercise64b},
AspectRatio -> 1/2,
PlotRange -> {{-5, 85}, {100, 130}},
PlotRangePadding -> Automatic,
Axes -> True, AxesLabel -> {x, y}, AxesOrigin -> {-5, 100},
ImageSize -> 400]

As well as Tooltips there are probably other less intrusive methods to
present the coordinates. One would be to draw faint lines to the coordinate
axes.

Graphics[
{{GrayLevel[0.9],
Line[{# {0, 1} - {5, 0}, #, # {1, 0} + {0, 111}}]} & /@
S11Exercise64b,
AbsolutePointSize[4],
Point[#] & /@ S11Exercise64b},
AspectRatio -> 1,
PlotRange -> {{-5, 85}, {111, 121}},
PlotRangePadding -> Automatic,
Frame -> True, FrameLabel -> {x, y},
FrameTicks -> Automatic,
ImageSize -> 400]

If the points are not so evenly spaced one could use data based ticks where
the tick labels and positions are at the point coordinate positions.

Another possibility is to present the data in both a plot and in a formatted
table.


David Park
djm...@comcast.net
http://home.comcast.net/~djmpark/index.html

Bill Rowe

unread,
Jul 5, 2012, 6:15:23 AM7/5/12
to
On 7/4/12 at 3:29 AM, sandwa...@gmail.com (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}};

<code snipped>

Try something like this:

ListPlot[S11Exercise64b, AxesLabel -> {x, y}, AxesOrigin -> {0, 0},
PlotStyle -> {Blue, PointSize[.02]},
Epilog -> {Text[#, {1, .95} #] & /@
S11Exercise64b[[2 ;; -1 ;; 2]]}]

I've set this to label every other point since labeling each
point makes it difficult to read the labels. If you need to see
coordinates for every point in a plot with many points, look at
using ToolTip to display the coordinates of a point when you
hover your mouse over that point.

Note, the usage of PlotStyle. It makes no sense to have ListPlot
plot the data and use the Epilog directive to re-plot the same
data with a larger point size or different color. You are
essentially plotting the data twice. It is far more efficient to
use the directive PlotStyle to specify point size, color etc. so
that data points are plotted once with the desired attributes.

Or if you wanted a larger font for the labels

ListPlot[S11Exercise64b, AxesLabel -> {x, y}, AxesOrigin -> {0, 0},
PlotStyle -> {Blue, PointSize[.02]},
Epilog -> {Text[Style[#,16], {1, .95} #] & /@
S11Exercise64b[[2 ;; -1 ;; 2]]}]

Or to use () instead of {}

ListPlot[S11Exercise64b, AxesLabel -> {x, y}, AxesOrigin -> {0, 0},
PlotStyle -> {Blue, PointSize[.02]},
Epilog -> {Text[
Style["(" <> StringTake[ToString@#, {2, -2}] <> ")",
16], {1, .9} #] & /@ S11Exercise64b[[2 ;; -1 ;; 2]]}]


Bob Hanlon

unread,
Jul 5, 2012, 6:15:53 AM7/5/12
to
If you put the axes origin at {0, 0} it is difficult to read the labels.

S11Exercise64b = {{0, 120}, {10, 119}, {20, 118}, {30, 117}, {40, 116}, {50,
115}, {60, 114}, {70, 113}, {80, 112}};

ListPlot[S11Exercise64b,
PlotStyle -> {Blue, AbsolutePointSize[6]},
FrameLabel -> {x, y},
Frame -> True,
Axes -> False,
PlotRange -> {{-4, 98}, {111, 121}},
Epilog -> {
Text[Style[#, 12], #, {-1.25, 0}] & /@
S11Exercise64b}]

Graphics[{
Text[Style[#, 12], #, {-1.25, 0}] & /@
S11Exercise64b,
Blue, AbsolutePointSize[6],
Point[S11Exercise64b]},
FrameLabel -> {x, y},
Frame -> True,
PlotRange -> {{-4, 98}, {111, 121}},
AspectRatio -> 1/GoldenRatio]


Bob Hanlon


On Wed, Jul 4, 2012 at 3:29 AM, Mike McCraith <sandwa...@gmail.com> 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},

Bob Hanlon

unread,
Jul 5, 2012, 6:16:24 AM7/5/12
to
S11Exercise64b =
{{0, 120}, {10, 119}, {20, 118}, {30, 117}, {40, 116},
{50, 115}, {60, 114}, {70, 113}, {80, 112}};

ListPlot[S11Exercise64b,
PlotStyle -> {Blue, AbsolutePointSize[6]},
FrameLabel -> {"x", "y"},
Frame -> True,
Axes -> False,
PlotRange -> {{-4, 98}, {111, 121}},
Epilog -> {Text[
Style["(" <> ToString[#[[1]]] <> ", " <>
ToString[#[[2]]] <> ")", 12], #, {-1.25, 0}] & /@
S11Exercise64b}]


Bob Hanlon


On Wed, Jul 4, 2012 at 12:26 PM, Mike McCraith <sandwa...@gmail.com> wrote:
> Thanks much!
>
> My remaining question is then is it possible to have the ordered pair read
> as (0, 120) instead of {0, 120}?
--
Bob Hanlon

Tomas Garza

unread,
Jul 5, 2012, 6:18:26 AM7/5/12
to

Forget about Do's and For's. Try this:
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[.01],Point[S11Exercise64b],Style[Text[ToString[#],#,{0,2}],8,"Label"]&/@S11Exercise64b},PlotRange->{{-5,85},{-5,125}}]
But I'd rather use Tooltip which will show the coordinates of the points when you place the cursor over each of them.
S11Exercise64b = {{0, 120}, {10, 119}, {20, 118}, {30, 117}, {40, 116}, {50,

115}, {60, 114}, {70, 113}, {80, 112}};
ListPlot[Tooltip[S11Exercise64b],

AxesLabel -> {x, y}, AxesOrigin -> {0, 0},

Epilog -> {Blue, PointSize[.01], Point[S11Exercise64b]},

PlotRange -> {{-5, 85}, {-5, 125}}]
In this way you obtain a less cluttered graph.
-Tomas
> Date: Wed, 4 Jul 2012 03:29:28 -0400
> From: sandwa...@gmail.com
> Subject: Creating a Scatter Plot with Labeled Points
> To: math...@smc.vnet.net

Mike McCraith

unread,
Jul 5, 2012, 6:18:57 AM7/5/12
to
Thanks much!

My remaining question is then is it possible to have the ordered pair read
as (0, 120) instead of {0, 120}?

On Wed, Jul 4, 2012 at 6:55 AM, Bob Hanlon <hanlo...@gmail.com> wrote:

> If you put the axes origin at {0, 0} it is difficult to read the labels.
>
> S11Exercise64b = {{0, 120}, {10, 119}, {20, 118}, {30, 117}, {40, 116},
> {50,
> 115}, {60, 114}, {70, 113}, {80, 112}};
>
> ListPlot[S11Exercise64b,
> PlotStyle -> {Blue, AbsolutePointSize[6]},
> FrameLabel -> {x, y},
> Frame -> True,
> Axes -> False,
> PlotRange -> {{-4, 98}, {111, 121}},
> Epilog -> {
> Text[Style[#, 12], #, {-1.25, 0}] & /@
> S11Exercise64b}]
>
> Graphics[{
> Text[Style[#, 12], #, {-1.25, 0}] & /@
> S11Exercise64b,
> Blue, AbsolutePointSize[6],
> Point[S11Exercise64b]},
> FrameLabel -> {x, y},
> Frame -> True,
> PlotRange -> {{-4, 98}, {111, 121}},
> AspectRatio -> 1/GoldenRatio]
>
>
> Bob Hanlon
>
>
> On Wed, Jul 4, 2012 at 3:29 AM, Mike McCraith <sandwa...@gmail.com>
> wrote:
> >
--20cf303ea694116ab604c4038151
Content-Type: text/html; charset="ISO-8859-1"
Content-Transfer-Encoding: quoted-printable
X-Sun-Content-Length: 3608

Thanks much!<div><br></div><div>My remaining question is then is it possibl=
e to have the ordered pair read as (0, 120) instead of {0, 120}?<br><br><di=
v class="gmail_quote">On Wed, Jul 4, 2012 at 6:55 AM, Bob Hanlon <span di=
r="ltr">&lt;<a href="mailto:hanlo...@gmail.com" target="_blank">han=
lon...@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">If you put the axes origin at {0, 0} it is d=
ifficult to read the labels.<br>
<div class="im"><br>
S11Exercise64b = {{0, 120}, {10, 119}, {20, 118}, {30, 117}, {40, 116}, {=
50,<br>
115}, {60, 114}, {70, 113}, {80, 112}};<br>
<br>
ListPlot[S11Exercise64b,<br>
</div> PlotStyle -&gt; {Blue, AbsolutePointSize[6]},<br>
<div class="im"> FrameLabel -&gt; {x, y},<br>
</div> Frame -&gt; True,<br>
Axes -&gt; False,<br>
PlotRange -&gt; {{-4, 98}, {111, 121}},<br>
Epilog -&gt; {<br>
Text[Style[#, 12], #, {-1.25, 0}] &amp; /@<br>
S11Exercise64b}]<br>
<br>
Graphics[{<br>
Text[Style[#, 12], #, {-1.25, 0}] &amp; /@<br>
S11Exercise64b,<br>
Blue, AbsolutePointSize[6],<br>
Point[S11Exercise64b]},<br>
<div class="im"> FrameLabel -&gt; {x, y},<br>
</div> Frame -&gt; True,<br>
PlotRange -&gt; {{-4, 98}, {111, 121}},<br>
AspectRatio -&gt; 1/GoldenRatio]<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
Bob Hanlon<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
<br>
On Wed, Jul 4, 2012 at 3:29 AM, Mike McCraith &lt;<a href="mailto:sandwat=
erh...@gmail.com">sandwa...@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt; Hello there. I&#39;m trying to graph a Scatter Plot based on a give=
n list of<br>
&gt; data and have the coordinates appear near the point.<br>
&gt;<br>
&gt; I&#39;m trying to save time and, instead of manually typing the text f=
or the<br>
&gt; coordinates, I&#39;d like to be able to pull the coordinates from the =
list to<br>
&gt; plot them point and list the text. Here&#39;s what I have so far:<b=
r>
&gt;<br>
&gt; S11Exercise64b = {{0, 120}, {10, 119}, {20, 118}, {30, 117}, {40, 11=
6},<br>
&gt; {50, 115}, {60, 114}, {70, 113}, {80, 112}};<br>
&gt; ListPlot[S11Exercise64b, AxesLabel -&gt; {x, y}, AxesOrigin -&gt; {0, =
0},<br>
&gt; Epilog -&gt; {Blue, PointSize[.02], Point[S11Exercise64b],<br>
&gt; Text[Style[<br>
&gt; &quot;(&quot; &amp;&amp; S11Exercise64b[[1, 1]] &amp;&amp; =
&quot;,&quot; &amp;&amp; S11Exercise64b[[1, 2]] &amp;&amp; &quot;)&quot;,<b=
r>
&gt; FontSize -&gt; 18], {.9*S11Exercise64b[[1, 1]] + 30, .9*S11Exercise64b=
[[1, 2]]<br>
&gt; + .2}],<br>
&gt; }<br>
&gt; ]<br>
&gt;<br>
&gt;<br>
&gt; Please note, for this example, I am the first point (0, 120).<br>
&gt;<br>
&gt; The line that begins with the &quot;(&quot; &amp;&amp; S11Exercise64b.=
..is my attempt to<br>
&gt; extract the coordinates from the given list. Thus, next to the poin=
t, it<br>
&gt; should read (0,120). However, I am getting ( ^ 0^, ^ 120 ^). Bas=
ically,<br>
&gt; my lack of knowledge concerning strings is one of my issues.<br>
&gt;<br>
&gt; But, as you can see, once fixed, I&#39;d have to create a Text[Style[ =
line for<br>
&gt; every ordered pair that I have in the list. Is there a way to autom=
ate<br>
&gt; this process? Again, a lack of know-how leads to now knowing how to=
do For<br>
&gt; loops.<br>
&gt;<br>
&gt; Any help is always greatly appreciated.<br>
&gt;<br>
&gt; Thanks in advance!<br>
&gt;<br>
&gt; Mike<br>
</div></div></blockquote></div><br></div>

--20cf303ea694116ab604c4038151--

0 new messages