Thursday, 15 August 2013

Labelling (pinning) points in a plot automatically

Labelling (pinning) points in a plot automatically

I have a scatter plot that I am generating based on some data stored in
comma-separated format. I need to add a label to each point: this
information is available as part of the .csv file as it's used as the row
labels. Currently, the approach I have is to plot the points automatically
but to add the labels by hand:
\RequirePackage{filecontents}
\begin{filecontents*}{\jobname.csv}
Label,x,y,angle
a,1,1,0
b,2,2.5,0
c,3,2.9,0
d,4,4.1,-90
\end{filecontents*}
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot table
[col sep = comma, x = x, y = y] {\jobname.csv};
\node[pin = a] at (axis cs:1,1) {};
\node[pin = b] at (axis cs:2,2.5) {};
\node[pin = c] at (axis cs:3,2.9) {};
\node[pin = -90:d] at (axis cs:4,4.1) {};
\end{axis}
\end{tikzpicture}
\end{document}
Clearly this is not very efficient: ideally, I'd like to read the
information from the stored table. This needs some form of mapping, but
from the documentation for pgfplots(table) there seems to be a lack of a
row-wise function to do that.
Is it possible to write a (relatively simply) mapping which will add the
nodes, picking up the x and y values plus the label and angle (or perhaps
wider 'pin modification') from the .csv file? My table is not too long:
maximum of about a dozen rows.

No comments:

Post a Comment