# Colored circles from data

I have a data file which gives x-position (xpos), radius, and color. I wish to draw a circle at ever (xpos,0) with appropriate radius and color. How do I achieve that?

I have used two approaches. The first is based on \addplot table, where I can get the required coloring by using meta. The problem in this approach is the correct circle size.

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{filecontents}{data.out}
0       1        2
1       3        4
2       5        6
3       7        8
4       11      12
5       13      14
\end{filecontents}

\begin{document}
\begin{tikzpicture}

\begin{axis}[
colorbar,
]

\addplot+[scatter,only marks,mark=*,point meta=explicit] table [x index={0},y expr=0,meta
index={2}] \datatable;

\end{axis}

\end{tikzpicture}
\end{document}


In an alternative approach, I have experimented with loops and pgfplotstable, which gets the circle size correct. The problem in this approach is the correct coloring of the circles. I also don't know how to correctly place and scale the axis. And if I put the \draw commands inside the axis environment, nothing gets drawn.

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{filecontents}{data.out}
0       1        2
1       3        4
2       5        6
3       7        8
4       11      12
5       13      14
\end{filecontents}

\begin{document}
\newcommand{\myxpos}{}
\newcommand{\mycolor}{}
\begin{tikzpicture}

\pgfplotstablegetrowsof{\datatable}
\pgfmathparse{\pgfplotsretval-1}
\pgfplotsinvokeforeach{1,...,\pgfmathresult} {
\pgfplotstablegetelem{#1}{0}\of\datatable
\pgfmathsetmacro{\myxpos}{\pgfplotsretval}

\pgfplotstablegetelem{#1}{1}\of\datatable

\pgfplotstablegetelem{#1}{2}\of\datatable
\pgfmathsetmacro{\mycolor}{\pgfplotsretval}
}

\begin{axis}[
anchor=origin,
colorbar,
xmin=-10,
xmax=-20,
ymin=-15,
ymax=15,
]

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


I think a problem here is that \draw circle is only available as a tikz command, with no counterpart in pgfplots. This forbids many of the simple color operations available in pgfplots.

Here is the desired output, created with matlab and matlab2tikz.

This is why you need to use visualization depends on key and its variants (the manual is pretty clear on this point) with the right marker type:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ymin=-1,ymax=1,
colorbar,
scatter/@pre marker code/.append style={/tikz/mark size=\myrad cm},
clip marker paths=true
]
\pgfplotsextra{\show\pgfplotsunitxlength}
table [x index={0},y expr=0,meta index={2}]
{
0       1        2
1       3        4
2       5        6
3       7        8
4       11      12
5       13      14
};
\end{axis}
\end{tikzpicture}
\end{document}


• Thanks, percusse. Your solution works, but the radii are not quantitatively correct. Their relative size is ok, but their absolute size is not. This is especially apparent, if one tries to set ymin and ymax. – Jost Jul 12 '15 at 14:22
• @Jost They are taken as dimensions without a unit the default is cm. If you want to be consistent you need to scale them with the unit vectors of the axes but then you need axis equal etc. to avoid ellipses. – percusse Jul 12 '15 at 14:36
• Could you point out, how the scaling is done? – Jost Jul 12 '15 at 14:41
• @Jost What is the resulting dimension of the circle? In accordance with x or y axis? What does the number in the table represent? – percusse Jul 12 '15 at 14:45
• The radius is given in the same dimensions as the x position. This means the first circle goes through the origin of the second circle at (1,0)`. – Jost Jul 12 '15 at 14:50