%20%E3%81%AE%E3%83%97%E3%83%AD%E3%83%83%E3%83%88%E3%81%AE%E4%B8%8B%E3%81%AE%E8%A4%87%E6%95%B0%E3%81%AE%E9%A0%98%E5%9F%9F%E3%81%AB%E6%9C%80%E9%81%A9%E3%81%AA%E6%96%B9%E6%B3%95%E3%81%AF%E4%BD%95%E3%81%A7%E3%81%99%E3%81%8B%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 のプロットを 2 つの領域に分割せずに、この図を描く他の方法 (よりエレガントな方法) はありますか? また、各部分に x1 と x2 を作成しない方法はありますか。
前もって感謝します。
答え1
soft 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}