pgfplots條件資料過濾

pgfplots條件資料過濾

我想知道是否有一種方法可以過濾 \addplot 參數中的數據點。我有一個資料集(請參閱下面的 MWE),其值對應於三個條件 (C) 之一。我希望能夠使用 pgfplots 和三個單獨的 \addplot 命令(每個 C 值一個)來讀取完整的資料集,每個命令都會過濾掉與該 C 值不對應的行,並用單獨的標記等。可能嗎?

我已經查看了skip coords between indexand x filter/filter point選項,但由於這些是函數的參數axis(因此改變了整個圖),這些似乎並沒有達到我想要的效果。

這是一個 MWE,顯示了我想要實現的目標:

\documentclass{standalone}

\usepackage{pgfplots}

\usepackage{filecontents}

\begin{filecontents}
C   P1      P2      P3  sigP3
1   1.12E-4 0.06189 0.1865  0.0080
1   6.03E-3 0.00000 0.2517  0.0046
1   2.64E-2 0.00000 0.2247  0.0165
2   6.49E-5 0.00000 0.1906  0.0043
2   1.27E-5 0.00296 0.2120  0.0039
2   3.34E-4 0.00865 0.1709  0.0050
3   1.59E-2 0.01857 0.1596  0.0216
3   7.10E-4 0.01857 0.2547  0.0316
3   3.23E-5 0.00526 0.1557  0.0051
3   2.33E-4 0.01857 0.2008  0.0136
3   5.80E-4 0.01857 0.2389  0.0172
\end{filecontents}

\begin{document}

\begin{tikzpicture}
  \pgfplotsset{legend cell align=right,legend style={font=\footnotesize,legend pos=outer north east}};
  \begin{semilogxaxis}[
      enlargelimits=false,axis on top,
      width=12cm,height=8cm,
      xlabel={$\Pi_1$},
      ylabel={$\Pi_3$},
      ymin=0.1,ymax=0.35,
      xmin=1E-5,xmax=1E-1,
    ]

    %% PLOT ONLY IF C = 1
    \addplot+[only marks,error bars/.cd,y dir=both,y explicit] coordinates {
      (1.12E-4,0.1865) +- (0,0.0080)
      (6.03E-3,0.2517) +- (0,0.0046)
      (2.64E-2,0.2247) +- (0,0.0165)
    };
    \addlegendentry{Cond. 1}

    %% PLOT ONLY IF C = 2
    \addplot+[only marks,error bars/.cd,y dir=both,y explicit] coordinates {
      (6.49E-5,0.1906) +- (0,0.0043)
      (1.27E-5,0.2120) +- (0,0.0039)
      (3.34E-4,0.1709) +- (0,0.0050)
    };
    \addlegendentry{Cond. 2}

    %% PLOT ONLY IF C = 3
    \addplot+[only marks,error bars/.cd,y dir=both,y explicit] coordinates {
      (1.59E-2,0.1596) +- (0,0.0216)
      (7.10E-4,0.2547) +- (0,0.0316)
      (3.23E-5,0.1557) +- (0,0.0051)
      (2.33E-4,0.2008) +- (0,0.0136)
      (5.80E-4,0.2389) +- (0,0.0172)
    };  
    \addlegendentry{Cond. 3}

  \end{semilogxaxis}
\end{tikzpicture}

\end{document}

答案1

您可以使用restrict expr to domain

  \begin{filecontents*}{mydata.dat}
  C   P1      P2      P3  sigP3
  1   1.12E-4 0.06189 0.1865  0.0080
  1   6.03E-3 0.00000 0.2517  0.0046
  1   2.64E-2 0.00000 0.2247  0.0165
  2   6.49E-5 0.00000 0.1906  0.0043
  2   1.27E-5 0.00296 0.2120  0.0039
  2   3.34E-4 0.00865 0.1709  0.0050
  3   1.59E-2 0.01857 0.1596  0.0216
  3   7.10E-4 0.01857 0.2547  0.0316
  3   3.23E-5 0.00526 0.1557  0.0051
  3   2.33E-4 0.01857 0.2008  0.0136
  3   5.80E-4 0.01857 0.2389  0.0172
  \end{filecontents*}

  \documentclass[margin=10pt]{standalone}
  \usepackage{pgfplots}
  \pgfplotsset{compat=1.12}
  \begin{document}
  \begin{tikzpicture}
    \pgfplotsset{
      legend cell align=right,
      legend style={font=\footnotesize,legend pos=outer north east}}
    \begin{semilogxaxis}[
        enlargelimits=false,axis on top,
        width=12cm,height=8cm,
        xlabel={$\Pi_1$},
        ylabel={$\Pi_3$},
        ymin=0.1,ymax=0.35,
        xmin=1E-5,xmax=1E-1,
        log basis x=10,
      ]
      \addplot+[only marks,error bars/.cd,y dir=both,y explicit]
        table[x=P1,y=P3,y error=sigP3,
        restrict expr to domain={\thisrow{C}}{1:1}
        ]{mydata.dat}; 
      \addlegendentry{Cond. 1}
      \addplot+[only marks,error bars/.cd,y dir=both,y explicit]
        table[x=P1,y=P3,y error=sigP3,
        restrict expr to domain={\thisrow{C}}{2:2}
        ]{mydata.dat}; 
      \addlegendentry{Cond. 2}
      \addplot+[only marks,error bars/.cd,y dir=both,y explicit]
        table[x=P1,y=P3,y error=sigP3,
        restrict expr to domain={\thisrow{C}}{3:3}
        ]{mydata.dat}; 
      \addlegendentry{Cond. 3}
    \end{semilogxaxis}
  \end{tikzpicture}
  \end{document}

在此輸入影像描述

或與\pgfplotsinvokeforeach

  \documentclass[margin=10pt]{standalone}
  \usepackage{pgfplots}
  \pgfplotsset{compat=1.12}
  \begin{document}
  \begin{tikzpicture}
    \pgfplotsset{
      legend cell align=right,
      legend style={font=\footnotesize,legend pos=outer north east}}
    \begin{semilogxaxis}[
        enlargelimits=false,axis on top,
        width=12cm,height=8cm,
        xlabel={$\Pi_1$},
        ylabel={$\Pi_3$},
        ymin=0.1,ymax=0.35,
        xmin=1E-5,xmax=1E-1,
        log basis x=10,
      ]
    \pgfplotsinvokeforeach{1,2,3}{ 
        \addplot+[only marks,error bars/.cd,y dir=both,y explicit]
          table[x=P1,y=P3,y error=sigP3,
          restrict expr to domain={\thisrow{C}}{#1:#1}
          ]{mydata.dat}; 
        \addlegendentry{Cond. #1};
     } 
    \end{semilogxaxis}
  \end{tikzpicture}
  \end{document}

相關內容