8

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?

9

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

|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 '16 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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