3

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}
\pgfplotsset{compat=newest}

\begin{filecontents}{data.out}
xpos    radius  color
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,
    ]

    \pgfplotstableread{data.out}\datatable

    \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}
\pgfplotsset{compat=newest}

\begin{filecontents}{data.out}
%xpos    radius  color
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{\myrad}{}
  \newcommand{\mycolor}{}
\begin{tikzpicture}

  \pgfplotstableread{data.out}\datatable

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

      \pgfplotstablegetelem{#1}{1}\of\datatable 
      \pgfmathsetmacro{\myrad}{\pgfplotsretval}

      \pgfplotstablegetelem{#1}{2}\of\datatable 
      \pgfmathsetmacro{\mycolor}{\pgfplotsretval}
      \draw[thick] (\myxpos,0) circle [radius=\myrad];
    }

  \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. Desired result, created with matlab and matlab2tikz

4

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,
  visualization depends on=\thisrow{radius}\as\myrad,
  scatter/@pre marker code/.append style={/tikz/mark size=\myrad cm},
  clip marker paths=true
]
\pgfplotsextra{\show\pgfplotsunitxlength}
\addplot+[scatter,only marks,mark=o,point meta=explicit,ultra thick]
    table [x index={0},y expr=0,meta index={2}]
{
xpos    radius  color
0       1        2
1       3        4
2       5        6
3       7        8
4       11      12
5       13      14
};    
\end{axis}  
\end{tikzpicture}    
\end{document}

enter image description here

  • 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

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.