5

I am working with pgfplots to build a scatter plot from data in a table, and would like a few formatting parameters to get picked up from the data table. I know that a single one such parameter can be accessed through using the visualisation depends on command

In order to smuggle more than one argument, I've tried including in the table a column of comma-separated strings containing actual arguments

visualization depends on = {value \thisrowno{3} \as \addArgs}

with the purpose of accessing those as substrings of \addArgs using a simple TeX macro

\def\strSplit#1#2{
\def\cntr@strSplit{0}\@for\currItm@strSplit:=#1
\do{\ifnum\cntr@strSplit=#2\currItm@strSplit\else\fi
\edef\cntr@strSplit{\the\numexpr\cntr@strSplit+1}\relax}}

Unfortunately, this command fails to deliver, even though it runs flawlessly when used outside pgfplots. I was wondering if you could help me. Please find below a self-contained .tex file with all code.

\documentclass[a4paper]{article}
\usepackage{tikz,pgf,pgfplots}

\makeatletter       
\def\strSplit#1#2{
    \def\cntr@strSplit{0}\@for\currItm@strSplit:=#1
    \do{\ifnum\cntr@strSplit=#2\currItm@strSplit\else\fi\edef\cntr@strSplit{\the\numexpr\cntr@strSplit+1}\relax}}
\def\currItm@strSplit{}
\makeatother

\begin{document}

\strSplit{sjfbvs,sifbiw,shdbas}1 %a test of the command

\begin{figure}[!h]
    \begin{tikzpicture}
        \begin{axis}[xlabel={\footnotesize$x$},
                xlabel style={at=(current axis.right of origin),anchor=south,yshift=16},
                xticklabel style={font=\footnotesize},
                ylabel={\footnotesize$y$},
                yticklabel style={font=\footnotesize},
                xmin=0,xmax=18,ymin=0,ymax=18,
                /pgf/number format/.cd,1000 sep={},
                axis y line=middle,axis x line=bottom,width=8cm,height=8cm,enlarge y limits=false]
            \addplot[scatter,only marks,mark=*,color=orange,fill=orange!50,
                nodes near coords=,point meta=explicit symbolic,
                visualization depends on = {value \thisrowno{3} \as \addArgs},
                nodes near coords style={anchor=center,
                pin={[pin distance=20,inner sep=0pt]\strSplit\addArgs0:
                \color{gray}\tiny\pgfplotspointmeta}}] 
                table[meta index=0,x index=1,y index=2,col sep=colon,row sep=\\]
            {
                label:x:y:args\\
                A:1:1:135,10\\
                B:2:2:315,10\\
                C:3:3:135,5\\
                D:4:4:315,5\\
                E:5:5:135,10\\
                F:6:6:315,10\\
                G:7:7:135,5\\
                H:8:8:315,5\\
                I:9:9:135,10\\
                J:10:10:315,10\\
                K:11:11:135,5\\
                L:12:12:315,5\\
                M:13:13:135,10\\
                N:14:14:315,10\\
                O:15:15:135,5\\
                P:16:16:315,5\\
            };
        \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

1 Answer 1

4

Fortunately, you don't need to smuggle since pgfplots allows more then one visualization depends on phrase. Thus you can make your args column into two and use them seperately:

visualization depends on = {value \thisrowno{3} \as \argi},
visualization depends on = {value \thisrowno{4} \as \argii},

Here as an example I additionally changed the pin distance depending on the fourth column:

enter image description here

The code:

\documentclass[a4paper]{article}
\usepackage{tikz,pgf,pgfplots}

\begin{document}

\begin{figure}[!h]
    \begin{tikzpicture}
        \begin{axis}[xlabel={\footnotesize$x$},
                xlabel style={at=(current axis.right of origin),anchor=south,yshift=16},
                xticklabel style={font=\footnotesize},
                ylabel={\footnotesize$y$},
                yticklabel style={font=\footnotesize},
                xmin=0,xmax=18,ymin=0,ymax=18,
                /pgf/number format/.cd,1000 sep={},
                axis y line=middle,axis x line=bottom,width=8cm,height=8cm,enlarge y limits=false]
            \addplot[scatter,only marks,mark=*,color=orange,fill=orange!50,
                nodes near coords=,point meta=explicit symbolic,
                visualization depends on = {value \thisrowno{3} \as \argi},
                visualization depends on = {value \thisrowno{4} \as \argii},
                nodes near coords style={anchor=center,
                    pin={[pin distance=\argii,inner sep=0pt]\argi:\color{gray}\tiny\pgfplotspointmeta}
                }
            ]
            table[meta index=0,x index=1,y index=2,col sep=colon,row sep=\\]
            {
                label:x:y:argi:argii\\
                A:1:1:135:10\\
                B:2:2:315:10\\
                C:3:3:135:5\\
                D:4:4:315:5\\
                E:5:5:135:10\\
                F:6:6:315:10\\
                G:7:7:135:5\\
                H:8:8:315:5\\
                I:9:9:135:10\\
                J:10:10:315:10\\
                K:11:11:135:5\\
                L:12:12:315:5\\
                M:13:13:135:10\\
                N:14:14:315:10\\
                O:15:15:135:5\\
                P:16:16:315:5\\
            };
        \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}
1
  • Wow, great! Thank you for a swift response, Mike! It's very good and useful to know about this functionality!
    – NDC
    Dec 1, 2017 at 0:28

You must log in to answer this question.

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