我有以下图表:
我怎样才能划出可行区域?
这是我的代码:
\begin{figure}[h]
\begin{center}
\begin{tikzpicture}
\begin{axis}[
grid,
grid style={densely dashed},
axis line style={->},
axis lines=middle,
xlabel=$x$,
ylabel=$y$,
xmin=-1, xmax=10,
ymin=-1, ymax=20,
axis lines=center,
axis on top=true,
domain=0:100,
]
\addplot [draw=red,thick] {15-2*x}; \addlegendentry{$6x + 3y \leq 45$}
\addplot [draw=blue,thick] (x,16-4*x); \addlegendentry{$4x + y \leq 16$}
\addplot [draw=green,thick] (x, 3*x); \addlegendentry{$3x \leq y$}
\end{axis}
\end{tikzpicture}
\captionof{figure}{}
\end{center}
\end{figure}
谢谢。
答案1
计算线的交点,然后填充中间部分,但将其分成两部分。
\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid,
grid style={densely dashed},
axis line style={->},
axis lines=middle,
xlabel=$x$,
ylabel=$y$,
xmin=-1, xmax=10,
ymin=-1, ymax=20,
axis lines=center,
axis on top=true,
domain=0:10,
]
\addplot [draw=red,thick,name path=A] {15-2*x}; \addlegendentry{$6x + 3y \leq 45$}
\addplot [draw=blue,thick,name path=B] (x,16-4*x); \addlegendentry{$4x + y \leq 16$}
\addplot [draw=green,thick,name path=C] (x, 3*x); \addlegendentry{$3x \leq y$}
\addplot[olive!20] fill between[of=C and A,soft clip={domain=0:0.51}];
\addplot[olive!20] fill between[of=B and C,soft clip={domain=0.5:16/7}];
\end{axis}
\end{tikzpicture}
\end{document}
您可能注意到,我没有使用0.5
但0.51
进行第一次填充,只是为了避免两部分之间出现一条空白的细线,这有时会出现在一些 PDF 查看器上(但不应该出现)。