
Tengo el siguiente problema. Leo datos de una tabla, los trazo y luego lleno el área entre el eje x y la curva. Esto funciona bien. Bueno, al menos un poco. Quiero que la "parte negativa" se rellene con un color diferente al de la "parte positiva" de la curva. Mi código se parece a esto
\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}
Resultado:
Respuesta1
No hay necesidad de fillbetween
biblioteca. Puede trazar el doble de sus datos pero recortando el resultado por encima o por debajo del eje 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}
Respuesta2
La fillbetween
biblioteca necesita la split
opción si debe aplicar diferentes estilos para diferentes segmentos. Además, el segundo camino ( B
en su ejemplo) debe tener el mismo ancho que el primer camino. En su ejemplo, B
abarca solo de x=0 a x=1, aunque los datos de entrada abarcan de 0 a 140.
Aquí hay un ejemplo con 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}
Tenga en cuenta que fillbetween
en realidad falla si usa every even segment/.style
: aparentemente, cuenta el área "vacía" como y=0
su propio segmento. Solucioné este problema mediante índices de segmento proporcionados explícitamente (no muy generales, de acuerdo...)
Tenga en cuenta que \path[name path=B] (0,0) -- (150,0);
es correcto: utiliza pgfplots
unidades (comenzando con compat=1.11
). Las versiones anteriores necesitan (axis cs:0,0) -- (axis cs:150,0)
.