pgfplots:從資料檔案為每個國家繪製一個子圖

pgfplots:從資料檔案為每個國家繪製一個子圖

我有國家/地區/年份級別的 csv 格式的數據,並且使用 pgfplots 我想隨時間繪製給定變量,每個國家一個子圖。我的想法是使用一個groupplot環境:以及一個內部循環,該循環將遍歷一組國家/地區,並「過濾掉」資料以僅保留一個國家。

問題是我什至無法過濾資料以僅保留一個國家。這是我的(非)工作範例,改編自此問題,嘗試過濾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}

我懷疑它不起作用,因為我正在嘗試過濾字串列? (包含國家的那個。)但對此甚至不確定。 Anwyay,我希望能得到一些關於過濾問題的建議,或者一些關於更優雅的方法來處理這些國家子圖的建議。

答案1

首先定義一個字串如下:

\def\AUTstring{AUT}

然後像這樣寫你的濾鏡

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

也就是說,如果\pgfplotsretvalAUT則不執行任何操作。否則就放棄這一點。

相關內容