TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to produce a scatter plot from a data file. The data file could look like this:

 x       y       color   myvalue
 0.5     0.2     1       test
 0.2     0.1     2       uniform

I'd like to add the string data in column myvalue to the nodes, but still have the option to colour the marks. I tried two things recommended in the manual:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[% scatter/use mapped color={draw=black,fill=mapped color},
              enlargelimits=0.2]
    \addplot[scatter, mark=*,only marks, 
            scatter src=explicit symbolic,
            nodes near coords,]
      table [x=x, y=y, meta=myvalue] {tab.tbl};

   \end{axis}
\end{tikzpicture}
\end{document}

This allows me to put the labels to the marker in the plot.

first attempt

But I don't know how to add colours. My second attempt was to use the colour for the point meta data (manual page 88), but then I couldn't add text (it would only accept numbers in the failing code below):

% same preamble 
\begin{tikzpicture}
  \begin{axis}[enlargelimits=0.2]
     \addplot[scatter, mark=*,only marks,
              scatter src=explicit symbolic,
           % we use ’point meta’ as color data...
           point meta=\thisrow{color},
           % ... therefore, we can’t use it as argument for nodes near coords ..
           nodes near coords*={\myvalue},
           % ... which requires to define a visualization dependency:
           visualization depends on={\thisrow{myvalue} \as \myvalue},
           ]
           table [x=x, y=y, meta=myvalue] {tab.tbl};
  \end{axis}
\end{tikzpicture}

How can I have both?

share|improve this question

oh, I found the solution in a related question.

How to use a string column to label datapoints in pgfplots.

The key is to use value in front of the macro definition.

The working code is:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[% scatter/use mapped color={draw=black,fill=mapped color},
              enlargelimits=0.2]
     \addplot[scatter, mark=*,only marks,
           % we use ’point meta’ as color data...
           point meta=\thisrow{color},
           % ... therefore, we can’t use it as argument for nodes near coords
           nodes near coords*={\myvalue},
           % ... which requires to define a visualization dependency:
           visualization depends on={value \thisrow{myvalue} \as \myvalue},
           ]
           table [x=x, y=y]
           {tab.tbl};
  \end{axis}
\end{tikzpicture}
\end{document}

colored

share|improve this answer
    
done. I created the pictures from inside a beamer documentclass, but standalone works as well and requires less overhead. – Sebastian Jan 11 at 19:49
    
Thanks! (+1) ;) – cfr Jan 11 at 21:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.