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圖書館。您可以繪製兩次數據,但將結果剪裁在 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此外,第二條路徑(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).

相關內容