3

I have data in csv format at the country-year level, and using pgfplots I would like to plot a given variable over time, with one subfigure per country. My idea was to use a groupplot environment: and a loop inside, that would loop over the set of countries, and "filter out" data to keep only one country.

The issue is that I am not even able to filter data to keep only one country. This is my (non)working example, adapted from this question, trying to filter on country == GER

\documentclass{standalone}
\usepackage{filecontents}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\usepackage{pgfplotstable}

\begin{filecontents}{CountryYears.csv}
    country,year,vartoplot
    AUT,1998,.1
    AUT,1999,.2
    AUT,2000,.7
    GER,1999,.4
    GER,2000,.45
    GER,2001,.7
    GER,2002,.6
    FRA,2000,.5
    FRA,2001,.75
    FRA,2002,.57894
    FRA,2003,.549
\end{filecontents}

\begin{document}
    \pgfplotstableset{columns/country/.style={string type}}
    \pgfplotstableread[col sep=comma]{CountryYears.csv}\loadeddata

      \begin{tikzpicture}
      \begin{axis}[
      x filter/.code={\pgfplotstablegetelem{\coordindex}{country}\of{\loadeddata}
        \pgfmathtruncatemacro{\tempva}{\pgfplotsretval == GER ? 1: 0}
        \ifnum\tempva>0%true
        \else%false
        \def\pgfmathresult{}
        \fi
      },
      ]
      \addplot[only marks] table[x=year,y=vartoplot] {\loadeddata};
      \end{axis}
      \end{tikzpicture}
\end{document}

I supsect it doesn't work because I'm trying to filter on a string column? (The one containing the country.) But not even sure about that. Anwyay, I would appreciate some pointers as to either the filtering problem, or perhaps some suggestion on a more elegant approach to doing these country subgraphs.

1

First define a string as follows:

\def\AUTstring{AUT}

and then write your filter like

    x filter/.code={
        \pgfplotstablegetelem{\coordindex}{country}\of{\loadeddata}
        \ifx\pgfplotsretval\AUTstring
        \else
            \pgfmathparse{inf}
        \fi
    }

That is, if \pgfplotsretval is AUT then do nothing. Otherwise drop this point.

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.