%20%E7%B9%AA%E5%9C%96%E4%B8%8B%E7%9A%84%E5%B9%BE%E5%80%8B%E5%8D%80%E5%9F%9F%EF%BC%8C%E6%9C%80%E5%A5%BD%E7%9A%84%E6%96%B9%E6%B3%95%E6%98%AF%E4%BB%80%E9%BA%BC%EF%BC%9F.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
您可以使用 asoft clip
來修復網域限制
\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}