我想问一个关于如何阴影化两条曲线之间的区域的问题。
首先下面是代码
\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\usepackage{mathtools}
\usepackage{pgfplots}
\usepackage{amsmath}
\newtheorem{theorem}{THEOREM}
\newtheorem{proof}{PROOF}
\usepackage{tikz}
\usepackage{amssymb}
\usetikzlibrary{patterns}
\usepackage{bigints}
\usepackage{color}
\usepgfplotslibrary{fillbetween}
\begin{document}
\setlength{\parindent}{0mm}
\pgfplotsset{every axis/.append style={
axis x line=middle, % put the x axis in the middle
axis y line=middle, % put the y axis in the middle
axis line style={<->}, % arrows on the axis
xlabel={$x$}, % default put x on x-axis
ylabel={$y=\sqrt{-x+1}$}, % default put y on y-axis
ticks=none
}}
% arrows as stealth fighters
\tikzset{>=stealth}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
xmin=-4,xmax=2,
ymin=-1,ymax=3,
]
\plot[name path=A, thick,samples=100,domain=-4:1] {sqrt(-1*x+1)};
\plot[name path=B,thick,samples=100,domain=-4:1] {2};
\addplot[fill=gray,opacity=.4,domain=0:1,samples=50] {sqrt(-1*x+1)}\closedcycle;
%\addplot[fill=gray, domain=-3:0,opacity=0.4] fill between[of=A and B];
\addplot+[mark=none,fill=gray,draw=black,domain=-3:0,opacity=0.4] {max(2-sqrt(-x+1),sqrt(-x+1))} \closedcycle;
\node [below] at (axis cs: 0.2,0) {$O$};
\draw[thick,dashed,brown] (axis cs:-3,0) -- (axis cs:-3,2);
\node [below] at (axis cs: -3,0) {$-3$};
\node [below] at (axis cs: 1,0) {$1$};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
因此,上述代码生成了以下图表。我实际上希望阴影区域位于 x=[-3,0] 上的水平线 (y=2) 和曲线 (y=\sqrt{-x+1}) 之间,而它只是阴影曲线和 x 轴下方的区域。
你是如何实现这一点的?
答案1
\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\usepackage{mathtools}
\usepackage{pgfplots}
\usepackage{amsmath}
\newtheorem{theorem}{THEOREM}
\newtheorem{proof}{PROOF}
\usepackage{tikz}
\usepackage{amssymb}
\usetikzlibrary{patterns}
\usepackage{bigints}
\usepackage{color}
\usepgfplotslibrary{fillbetween}
\begin{document}
\setlength{\parindent}{0mm}
\pgfplotsset{every axis/.append style={
axis x line=middle, % put the x axis in the middle
axis y line=middle, % put the y axis in the middle
axis line style={<->}, % arrows on the axis
xlabel={$x$}, % default put x on x-axis
ylabel={$y=\sqrt{-x+1}$}, % default put y on y-axis
ticks=none
}}
% arrows as stealth fighters
\tikzset{>=stealth}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
xmin=-4,xmax=2,
ymin=-1,ymax=3,
]
\plot[name path=A, thick,samples=100,domain=-4:1] {sqrt(-1*x+1)};
\plot[name path=B,thick,samples=100,domain=-4:1] {2};
\addplot[fill=gray,opacity=0.4] fill between[of=A and B,split,soft clip={domain=-3:0},every segment no 0/.style={white},];
\node [below] at (axis cs: 0.2,0) {$O$};
\draw[thick,dashed,brown] (axis cs:-3,0) -- (axis cs:-3,2);
\node [below] at (axis cs: -3,0) {$-3$};
\node [below] at (axis cs: 1,0) {$1$};
\node [above right] at (axis cs: 0,2) {$2$};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
为了填充曲线之间的区域,我们可以使用单独设置区域样式every segment no
,并在必要时应用,soft clip
就像您的情况一样[-3,0]。
答案2
像这样吗?
\addplot fill between[
of = func and line,
soft clip={domain=-3:0},
every even segment/.style = {gray,opacity=.4}
];
我稍微整理了一下你的代码。下面是一个完整的示例:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{every axis/.append style={
axis x line=middle, % put the x axis in the middle
axis y line=middle, % put the y axis in the middle
axis line style={<->}, % arrows on the axis
}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$}, % default put x on x-axis
ylabel={$y=\sqrt{-x+1}$}, % default put y on y-axis
xtick={-3,0,1},
ytick={2},
samples=100,
domain=-4:1,
xmin=-4,xmax=2,
ymin=-1,ymax=3,
]
\addplot[name path=func, black, thick, mark=none, ] {sqrt(-x+1)};
\addplot[name path=line,thick] {2};
\addplot fill between[
of = func and line,
soft clip={domain=-3:0},
every even segment/.style = {gray,opacity=.4}
];
\draw[thick,dashed,brown] (axis cs:-3,0) -- (axis cs:-3,2);
\end{axis}
\end{tikzpicture}
\end{document}
顺便提一句:
您不需要节点来放置轴刻度标签,而是使用xtick
和xtick
选项。
答案3
您可以使用soft clip
限制填充到域x=[-3,0]:
\addplot[fill=gray,opacity=0.4] fill between [of=A and B, soft clip={domain=-3:0}];
因此下面的代码
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{every axis/.append style={
axis x line=middle, % put the x axis in the middle
axis y line=middle, % put the y axis in the middle
axis line style={<->}, % arrows on the axis
xlabel={$x$}, % default put x on x-axis
ylabel={$y=\sqrt{-x+1}$}, % default put y on y-axis
ticks=none
}}
% arrows as stealth fighters
\tikzset{>=stealth}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
xmin=-4,xmax=2,
ymin=-1,ymax=3,
]
\plot[name path=A, thick,samples=100,domain=-4:1] {sqrt(-1*x+1)};
\plot[name path=B,thick,samples=100,domain=-4:1] {2};
\addplot[fill=gray,opacity=0.4] fill between [of=A and B, soft clip={domain=-3:0}];
\node [below] at (axis cs: 0.2,0) {$O$};
\draw[thick,dashed,brown] (axis cs:-3,0) -- (axis cs:-3,2);
\node [below] at (axis cs: -3,0) {$-3$};
\node [below] at (axis cs: 1,0) {$1$};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
给出输出