8

How do I set the color of a marker with one meta column in a table AND its size with another meta column?

So in this minimal working example, how can I set the marker size with 'w1' and the color with 'w2'?

\begin{filecontents*}{temp.dat}
x y w1 w2
1 1 2 4
2 2 1 3
3 3 4 2
4 4 3 1
\end{filecontents*}

\documentclass[10pt,a4paper]{report}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}[]
\addplot[scatter,
only marks,
scatter src=explicit,
scatter/@pre marker code/.append style={/tikz/mark size=2+\pgfplotspointmetatransformed/300}]
table[x=x,y=y,meta=w1]{temp.dat};
\end{axis}
\end{tikzpicture}

\end{document}

1 Answer 1

14

You can use the visualization depends on=<expr>\as<macro> key for to make another variable available. If you set visualization depends on=\thisrow{w2}\as\wtwo, the content of the w2 column will be available as a macro called \wtwo:

\begin{filecontents*}{temp.dat}
x y w1 w2
1 1 2 4
2 2 1 3
3 3 4 2
4 4 3 1
\end{filecontents*}

\documentclass[10pt,a4paper]{report}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}[]
\addplot[
    scatter,
    only marks,
    scatter src=explicit,
    visualization depends on=\thisrow{w2}\as\wtwo,
    scatter/@pre marker code/.append style={
        /tikz/mark size=\wtwo
    }
]
table[x=x,y=y,meta=w1]{temp.dat};
\end{axis}
\end{tikzpicture}

\end{document}
7
  • Why did you put the filecontents* before the documentclass? Feb 17, 2013 at 21:22
  • @GarbageCollector: Because that's what the OP did. But you're right, I was surprised by that as well. Seems to work fine though.
    – Jake
    Feb 17, 2013 at 21:24
  • There is a drawback if we put the filecontents* before the document class because TeX is unable to rewrite the temp.dat even though \usepackage{filecontents} exists in the preamble. Feb 17, 2013 at 21:37
  • @GarbageCollector: Yeah, I know. That's not the case here, though.
    – Jake
    Feb 17, 2013 at 21:37
  • 1
    By design, in the beginning filecontents environment can only be used before the \documentclass and it cannot overwrite the existing file. But later the developers made a hacked-up version by providing us with filecontents package. This package must be loaded if we want to let TeX overwrite the existing file. No need to load filecontents package if you just want to invoke filecontents environment in the preamble. Feb 18, 2013 at 5:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .