Tikz addplot は符号に応じて塗りつぶします

Tikz addplot は符号に応じて塗りつぶします

次のような問題があります。テーブルからデータを読み取り、それをプロットして、X 軸と曲線の間の領域を塗りつぶします。これでうまくいきます。少なくとも少しは。曲線の「負の部分」を「正の部分」とは別の色で塗りつぶしたいのです。これまでのところ、私のコードは次のようになっています。

\documentclass[paper=a4,fontsize=12pt,open=any,numbers=noenddot]{scrreprt} 

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{figure}
\centering
    \begin{tikzpicture} 
        \begin{axis}
            [xlabel={$t$ [s]},ylabel={y}, xmin=0, xmax=140, ymin=-0.6, ymax=1.2, grid, width=14.5cm, height=7cm]
            \addplot plot [name path=A, color=black, mark=no] table{test.txt};
            \addplot[name path=B,black,mark=no,line width=0.01pt] coordinates  {(0,0) (1,0)};
            \addplot[gray!40] fill between[of=A and B];
            \end{axis} 
    \end{tikzpicture}
\caption{test}
\label{fig:test}
\end{figure}
\end{document}

結果: ここに画像の説明を入力してください

答え1

ライブラリは必要ありませんfillbetween。データを 2 倍にしてプロットできますが、結果は y 軸の上または下に切り取られます。

\documentclass[paper=a4,fontsize=12pt,open=any,numbers=noenddot]{scrreprt} 
\usepackage{filecontents}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
%\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{filecontents}{test.txt}
0 0
16 0
16 0.125
44 0.125
44 0.25
56 0.25
56 -0.125
64 -0.125
64 0
80 0
80 0.125
104 0.125
104 0.25
116 0.25
116 -0.125
124 -0.125
124 0
140 0
\end{filecontents}

\begin{figure}
\centering
    \begin{tikzpicture} 
        \begin{axis}
            [xlabel={$t$ [s]},ylabel={y}, xmin=0, xmax=140, ymin=-0.6, ymax=1.2, grid, width=14.5cm, height=7cm]
%            \addplot plot [name path=A, color=black, mark=no] table{test.txt};
%            \addplot[name path=B,black,mark=no,line width=0.01pt, domain=0:140] {0};
            \begin{scope}
            \clip (axis cs:0,0) rectangle (axis cs:140,1.2);
            \addplot plot [color=black, mark=no,fill=red] table{test.txt}\closedcycle;
            \end{scope}
            \begin{scope}
            \clip (axis cs:0,0) rectangle (axis cs:140,-1.2);
            \addplot plot [color=black, mark=no,fill=green] table{test.txt}\closedcycle;
            \end{scope}
            \end{axis} 
    \end{tikzpicture}
\caption{test}
\label{fig:test}
\end{figure}
\end{document}

ここに画像の説明を入力してください

答え2

異なるセグメントに異なるスタイルを適用する場合、ライブラリにはオプションfillbetweenが必要ですsplit。さらに、2 番目のパス (Bこの例では) は最初のパスと同じ幅にする必要があります。この例では、B入力データは 0 から 140 までの範囲ですが、範囲は x=0 から x=1 までです。

以下に例を示しますfillbetween

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepgfplotslibrary{fillbetween}
\begin{document}
    \begin{tikzpicture} 
        \begin{axis}
            [xlabel={$t$ [s]},ylabel={y}, xmin=0, xmax=140, ymin=-0.6, ymax=1.2, grid, width=14.5cm, height=7cm]
            \addplot[name path=A, color=black] table{
0 0
16 0
16 0.125
44 0.125
44 0.25
56 0.25
56 -0.125
64 -0.125
64 0
80 0
80 0.125
104 0.125
104 0.25
116 0.25
116 -0.125
124 -0.125
124 0
140 0
            };
            \path[name path=B] (0,0) -- (150,0);
            \addplot[red] fill between[of=A and B,split,
                every segment no 1/.style={orange},
                every segment no 4/.style={orange},
            ];
            \end{axis} 
    \end{tikzpicture}
\end{document}

ここに画像の説明を入力してください

fillbetween:を使用すると、実際には が失敗することに注意してくださいevery even segment/.style。どうやら、 の「空の」領域がy=0独自のセグメントとしてカウントされるようです。私は、明示的に指定されたセグメント インデックスを使用してこの問題を回避しました (あまり一般的ではありませんが、同意します...)

\path[name path=B] (0,0) -- (150,0);は正しいことに注意してください。pgfplots単位 ( で始まるcompat=1.11) を使用します。古いバージョンでは が必要です(axis cs:0,0) -- (axis cs:150,0)

関連情報