填充由多条曲线界定的区域

填充由多条曲线界定的区域

我正在尝试绘制最大化问题的可行集。我已经能够绘制它的边界,但我很难对其进行着色。我知道我的fill between语句填充了 y 轴(从 0 到 1)和 f_2 界定的区域,但我完全不清楚语句的哪一部分实际上限制了阴影出现在 0 和 1 之间。我想修改语句,使其将阴影限制为 (Sqrt[14]/5,1),然后添加另一个阴影,以着色 y 轴和 f_5 (0,Sqrt[14]/5) 之间的区域。

或者,如果我能用白色填充 f_2 和 f_5 之间的区域(从交叉点向下),我会很高兴。为此,我尝试添加

\addplot[fill=none] fill between [of=constraint_2 and constraint_5, split, every segment no 1/.style={fill,blue},on layer={axis background}];

按照上一条fill between说明操作,确实解决了我的问题。不过,我觉得这个解决方案有点别扭。

当然,我也愿意接受其他更好的解决方案。以下是我的 MWE

\documentclass{article}
\usepackage{tikz,pgfplots}

\pgfplotsset{compat = newest}
\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{figure}
    \centering
    \footnotesize
    \begin{tikzpicture}
       \begin{axis}[
          xlabel={$q_1$},
          ylabel={$q_2$},
          every axis x label/.style={at={(ticklabel* cs:1)},anchor=west},
          every axis y label/.style={at={(ticklabel* cs:1)},anchor=south},
          ticks=none, 
          axis x line=bottom,
          axis y line=left,
          xmin=0, xmax=2.2,
          ymin=0, ymax=2,
          y axis line style={name path=yaxis},
          ]
       \addplot[
          name path=constraint_2,
          domain=0:sqrt(2),
          samples=200,
          y filter/.expression={x==sqrt(2)?0:y},
          thick,
          ] {sqrt(2-x^2)};
       \addplot[
          name path=constraint_5,
          domain=0:sqrt(122/75),
          samples=200,
          y filter/.expression={x==sqrt(122/75)?0:y},
          thick,
          ] {sqrt((122/25-3*x^2)/1)};
       \addplot[gray,opacity = 0.1] fill between [of=yaxis and constraint_2, reverse=false, soft clip={(-1,-1) rectangle (1,1)}, on layer={axis background}];
       \addplot[fill=none] fill between [of=constraint_2 and constraint_5, split, every segment no 1/.style={fill,blue},on layer={axis background}];
       \addplot[
          domain = 0:7,
          samples =2,
          thick
          ] {1};
       \node at (axis cs: 1,1.6) {$f_5$};
       \node at (axis cs: 0.45,1.47) {$f_2$};
       \node at (axis cs: 1.9,1.07) {$f_1$};
       \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

及其输出

在此处输入图片描述

答案1

我不完全明白你想要实现什么,但如果你想填充由两个轴 f1、f2 和 f5 定义的区域,使用常规\fill命令(结合辅助程序\path)和选项可能会更容易。intersection segments

以下代码首先创建一个命名路径,该路径将 f2 和 f5 的相关部分组合在一起,然后将此路径与 f1(我将其命名为constraint_3)组合在一起。由于您的绘图分别在 y = 0 和 x = 0 处开始和结束,因此很容易使用此组合路径创建一个以另一个点为原点的多边形并填充它:

\documentclass{article}
\usepackage{tikz,pgfplots}

\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{figure}
    \centering
    \footnotesize
    \begin{tikzpicture}
       \begin{axis}[
          xlabel={$q_1$},
          ylabel={$q_2$},
          every axis x label/.style={at={(ticklabel* cs:1)},anchor=west},
          every axis y label/.style={at={(ticklabel* cs:1)},anchor=south},
          ticks=none, 
          axis x line=bottom,
          axis y line=left,
          xmin=0, xmax=2.2,
          ymin=0, ymax=2,
          y axis line style={name path=yaxis},
          ]
       \addplot[
          name path=constraint_2,
          domain=0:sqrt(2),
          samples=200,
          y filter/.expression={x==sqrt(2)?0:y},
          thick,
          ] {sqrt(2-x^2)};
       \addplot[
          name path=constraint_5,
          domain=0:sqrt(122/75),
          samples=200,
          y filter/.expression={x==sqrt(122/75)?0:y},
          thick,
          ] {sqrt((122/25-3*x^2)/1)};
       \addplot[
          name path=constraint_3,
          domain = 0:7,
          samples = 2,
          thick
          ] {1};
       \path[name path=helper_path, intersection segments={of=constraint_2 and constraint_5, sequence={L1 -- R2}]}];
       \fill[gray, opacity=0.1, intersection segments={of=constraint_3 and helper_path, sequence={L1 -- R2}]}] -- (0,0) -- cycle;
       \node at (axis cs: 1,1.6) {$f_5$};
       \node at (axis cs: 0.45,1.47) {$f_2$};
       \node at (axis cs: 1.9,1.07) {$f_1$};
       \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

答案2

我不知道您描述的内容,所以我只是填充一些随机区域:

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=center, axis equal,
xlabel={$q_1$}, ylabel={$q_2$},
every axis x label/.style={at={(ticklabel* cs:1)},anchor=west},
every axis y label/.style={at={(ticklabel* cs:1)},anchor=south},
ticks=none, 
xmin=0, xmax=2.2,
ymin=0, ymax=2,
y axis line style={name path=yaxis},
]
\addplot[
name path=constraint_2,
domain=0:sqrt(2),
samples=200,
y filter/.expression={x==sqrt(2)?0:y},
thick,
] {sqrt(2-x^2)} node[above, pos=0.2]{$f_2$};
\addplot[
name path=constraint_5,
domain=0:sqrt(122/75),
samples=200,
y filter/.expression={x==sqrt(122/75)?0:y},
thick,
] {sqrt(122/25-3*x^2)} node[above right, pos=0.4]{$f_5$};
\addplot[
name path=constraint_1,
domain=0:2.2, samples=2, 
thick] {1} node[above, pos=0.8]{$f_1$};
\fill ({sqrt(14)/5},1) circle[radius=1.5pt] node[pin={[pin distance=1.2cm]40:{$\scriptscriptstyle \left(\frac{\sqrt{14}}{5} , 1\right)$}}]{};
\addplot[gray, opacity=0.5] fill between [of=yaxis and constraint_2, reverse=false];
\addplot[red, opacity=0.5] fill between [of=constraint_1 and constraint_2,
soft clip={(0,0) rectangle ({sqrt(14)/5},2)},
];
\addplot[fill=none] fill between [of=constraint_2 and constraint_5, split,
every segment no 1/.style={fill, green}
];
\addplot[blue, opacity=0.5] fill between [of=yaxis and constraint_2,
soft clip={(0,{sqrt(14)/5}) rectangle (2,1)},
];
\addplot[blue, opacity=0.5] fill between [of=yaxis and constraint_5,
soft clip={(0,0) rectangle (2,{sqrt(14)/5})},
];
\end{axis}
\end{tikzpicture}
\end{document}

包含多个填充区域的图形

相关内容