notRMS
notRMS

Reputation: 11

gnuplot: Is it possible to plot the (x, y) coordinate besides each point?

I found an answer here about using labels with the points but that only works for 1 column with the label, whereas in order to plot (x, y) along with the point I have to use 2.

So I need something along the lines of plot "data.txt" using ($1):($2):1:2 with labels, "data.txt" using 1:2 with ($1):($2):1:2 being (x):(y) the coordinate for the point on the image and 1:2 being the label, except that doesn't work since it isn't a valid syntax and so only the first value is plotted at the correct location.

Well I guess I can use something like plot "data.txt" using ($1+2):($2+2):1 with labels, "data.txt" using ($1+10):($2+2):2 with labels, "data.txt" using 1:2 to manually set the spacing but damn is that ugly/low-level/hackish/bad/etc.

Upvotes: 1

Views: 1773

Answers (1)

Woltan
Woltan

Reputation: 14033

how about this:

set key off
get_point(x,y) = sprintf("(%.0f,%.2f)", x, y)
offset(y) = (y<0.5) ? (y - 0.05) : (y + 0.05)

plot [-1:6] "-" u ($1):(offset($2)):(get_point($1, $2)) with labels, "-" u 1:2 w l
0 0.20323
1 0.19147
2 0.50213
3 0.17599
4 0.07732
5 0.66897
e
0 0.20323
1 0.19147
2 0.50213
3 0.17599
4 0.07732
5 0.66897
e

What it does:
With the get_point macro you can easily form a string which will be your x- and y-coordinates. The offset function is simply to move the labels a bit away from the curve. This might be useful if you data has a specific form (like in this case a global mean value of 0.5).
Of course the script looks nicer, if you use a datafile instead of having the data in the plot file.

Upvotes: 3

Related Questions