Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Probably an easy answer to this, but I can't get it to work, and I obviously don't understand the manual or the other answers given to similar questions here on tex.sx (see 1,2, and 3).

Basically I want to use the values of an additional column in a table to specify the size of the markers, but keeping the colour the same for each marker (e.g., a colour defined by \definecolor{name}{rgb}{x1,x2,x3}).

An MWE doing almost what I want; the size of the markers is controlled by the third column as desired, but obviously the colour as well. What do I need to change in order to keep the colour constant for all the markers?

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\usepackage{amsmath}

\begin{filecontents*}{temp.dat}
1   1   100
2   2   200
3   3   300
4   4   400
5   5   500
6   6   600
7   7   700
8   8   800
9   9   900
10  10  1000
\end{filecontents*}

\begin{document}
\begin{tikzpicture}

\begin{axis}[%
width=4.5in,
height=3.5in,
scale only axis,
xmin=0,
xmax=10,
ymin=0,
ymax=10,
axis x line*=bottom,
axis y line*=left
]
\addplot[%
scatter,
only marks,
mark=*,
color=blue,
visualization depends on={\thisrowno{2} \as \perpointmarksize},
scatter/@pre marker code/.append style={/tikz/mark size=\perpointmarksize/50}] plot table[row sep=crcr]{temp.dat};
\end{axis}
\end{tikzpicture}%
\end{document}
share|improve this question
    
Seems like adding scatter/use mapped color={draw=ColorX,fill=ColorY}, does the trick, but I'm not sure if this is the best solution. –  militiaOfDeath Feb 3 at 18:57
    
Should mention that for the previous comment to work as intended, the line has to be added prior to visualization depends on={\thisrowno{2} \as \perpointmarksize}. –  militiaOfDeath Feb 3 at 19:04

1 Answer 1

up vote 3 down vote accepted

The most straightforward way of letting the scatter style only change the size but not the colour of the marks is to add

scatter/@pre marker code/.style={/tikz/mark size=\pgfplotspointmeta/50},
scatter/@post marker code/.style={}

to the \addplot options. Note the use of .style instead of .append style. What happens when you load the scatter style is that, among other things, installs its own @pre marker code stuff that deals with changing the colour. By using @pre marker code/.style, you can overwrite that code with your own. Note that you'll also need to clear the @post marker code using /.style={} because otherwise it would contain an unmatched \endscope.

To be able to use the \pgfplotspointmeta macro for scaling the data points instead of a macro defined using visualization depends on, you need to set point meta=explicit symbolic in the \addplot options. This tells PGFPlots to expect a meta value for every data point, instead of simply using the y value. The symbolic keyword specifies that the value shouldn't be parsed and shouldn't be converted to floating point format, which would throw the standard math parser off.

\documentclass[tikz, border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\usepackage{amsmath}

\begin{filecontents*}{temp.dat}
1   1   100
2   2   200
3   3   300
4   4   400
5   5   500
6   6   600
7   7   700
8   8   800
9   9   900
10  10  1000
\end{filecontents*}

\begin{document}
\begin{tikzpicture}

\begin{axis}[%
    width=4.5in,
    height=3.5in,
    scale only axis,
    xmin=0,
    xmax=10,
    ymin=0,
    ymax=10,
    axis x line*=bottom,
    axis y line*=left
]
\addplot[%
    scatter=true,
    only marks,
    mark=*,
    color=blue,
    point meta=explicit symbolic,
    scatter/@pre marker code/.style={/tikz/mark size=\pgfplotspointmeta/50},
    scatter/@post marker code/.style={}
] table [meta index=2] {temp.dat};
\end{axis}
\end{tikzpicture}%
\end{document}
share|improve this answer
    
Thanks for the detailed answer. It seems a lot less complex than the method I tried. –  militiaOfDeath Feb 4 at 10:26

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.