
다음과 같은 문제가 있습니다. 테이블에서 데이터를 읽고 플롯한 다음 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)
.