%20%ED%94%8C%EB%A1%AF%20%EC%95%84%EB%9E%98%20%EC%97%AC%EB%9F%AC%20%EC%98%81%EC%97%AD%EC%97%90%20%EB%8C%80%ED%95%9C%20%EA%B0%80%EC%9E%A5%20%EC%A2%8B%EC%9D%80%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F.png)
코드:
\documentclass[dvipsnames]{article}
\usepackage{pgfplots}
\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
xmin = -1,
ymin = -3,
xmax = 6,
ymax = 4,
domain = -1:10,
xtick = {1,2,...,5},
ytick = \empty,
xlabel style={below right},
ylabel style={above left},
x tick label style={below},
xlabel = {$x$},
ylabel = {$f$},
]
\addplot[very thick, color=Mahogany, domain=0:2, name path=p1] {3 - x};
\addplot[very thick, color=Mahogany, domain=2:5, name path=p2] {3 - x};
\addplot[domain=0:2, name path=x1] {0};
\addplot[domain=2:5, name path=x2] {0};
\addplot [fill=orange] fill between[of = p1 and x1];
\addplot [fill=gray] fill between[of = p2 and x2];
\end{axis}
\end{tikzpicture}
\end{document}
두 영역에서 5-x의 플롯을 깨지 않고 이 그림을 그릴 수 있는 다른 (더 우아한) 방법이 있습니까? 그리고 각 부분에 대해 x1과 x2를 만들지 않고.
미리 감사드립니다.
답변1
soft clip
a를 사용하여 도메인 제한을 수정할 수 있습니다.
\documentclass[dvipsnames]{article}
\usepackage{pgfplots}
\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
xmin = -1,
ymin = -3,
xmax = 6,
ymax = 4,
domain = -1:10,
xtick = {1,2,...,5},
ytick = \empty,
xlabel style={below right},
ylabel style={above left},
x tick label style={below},
xlabel = {$x$},
ylabel = {$f$},
]
\addplot[very thick, color=Mahogany, domain=0:5, name path=p1] {3 - x};
% \addplot[very thick, color=Mahogany, domain=2:5, name path=p2] {3 - x};
\addplot[domain=0:5, name path=x1] {0};
% \addplot[domain=2:5, name path=x2] {0};
\addplot [fill=orange] fill between[of = p1 and x1, soft clip={domain=0:2}];
\addplot [fill=gray] fill between[of = p1 and x1, soft clip={domain=2:5}];
\end{axis}
\end{tikzpicture}
\end{document}