Tikz Addplot füllt zwischen abhängig vom Vorzeichen

Tikz Addplot füllt zwischen abhängig vom Vorzeichen

ich habe folgendes Problem. Ich lese Daten aus einer Tabelle, stelle sie grafisch dar und fülle dann den Bereich zwischen der x-Achse und der Kurve aus. Das funktioniert gut. Zumindest ein bisschen. Ich möchte den „negativen Teil“ in einer anderen Farbe ausfüllen als den „positiven Teil“ der Kurve. Mein Code sieht bisher so aus

\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}

Ergebnis: Bildbeschreibung hier eingeben

Antwort1

Es besteht keine Notwendigkeit für fillbetweeneine Bibliothek. Sie können Ihre Daten zweimal darstellen, das Ergebnis jedoch über oder unter der Y-Achse abschneiden.

\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}

Bildbeschreibung hier eingeben

Antwort2

Die fillbetweenBibliothek benötigt diese splitOption, wenn sie für verschiedene Segmente unterschiedliche Stile anwenden soll. Außerdem Bsollte der zweite Pfad (in Ihrem Beispiel) dieselbe Breite wie der erste Pfad haben. In Ihrem Beispiel Breicht er nur von x=0 bis x=1, obwohl die Eingabedaten von 0 bis 140 reichen.

Hier ist ein Beispiel mit 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}

Bildbeschreibung hier eingeben

Beachten Sie, dass dies fillbetweentatsächlich fehlschlägt, wenn Sie verwenden every even segment/.style: Anscheinend zählt es den „leeren“ Bereich mit y=0als eigenes Segment. Ich habe dieses Problem umgangen, indem ich explizit Segmentindizes bereitgestellt habe (nicht sehr allgemein, stimmt...)

Beachten Sie, dass dies \path[name path=B] (0,0) -- (150,0);korrekt ist: Es werden pgfplotsEinheiten verwendet (beginnend mit compat=1.11). Ältere Versionen benötigen (axis cs:0,0) -- (axis cs:150,0).

verwandte Informationen