我对 Tikz 中的填充命令有疑问。
我的问题是虚线区域在所有方向上都超出/或缩小了。我希望虚线区域能够精确填充线周围的区域。
有没有什么帮助可以调整填充区域?谢谢
代码如下
\documentclass{report}
\usepackage{tikz,pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\draw[gray!50, thin, step=0.5] (-1,-1) grid (5,4);
\draw[very thick,->] (-1,0) -- (5,0) node[right] {$x_1$};
\draw[very thick,->] (0,-1) -- (0,5) node[above] {$x_2$};
% absice des axes
\foreach \x in {-1,...,5} \draw (\x,0.05) -- (\x,-0.05) node[below] {\tiny\x};
\foreach \y in {-2.5,...,4} \draw (-0.05,\y) -- (0.05,\y) node[right] {\tiny\y};
% \fill[blue!50!cyan,opacity=0.3] (8/3,1/3) -- (1,2) -- (13/3,11/3) -- cycle;
\draw[name path=line1] (2,4) -- node[above right,sloped] {\tiny$2x_A+x_B\leq8$} (4.5,-1); % desig une ligne
\fill [blue!50!cyan, above ,opacity=0.3, domain=-1:2, variable=\x]
(5,4)
-- plot ({\x}, {8 -2* \x})
-- (4.5,-1)
-- (5,-1)
-- cycle;
\draw[name path=line2] (-1,3) -- node[above ,sloped] {\tiny$x_B\geq3$} (5,3); % desig une ligne
\fill [green!50!cyan, above ,opacity=0.3, domain=-1:2, variable=\x]
(-1, 4)
-- plot ({\x}, {3})
-- (5, 4)
-- cycle;
\draw[name path=line3] (-1,4) -- node[below left,sloped] {\tiny$x_1+2x_B\leq7$} (5,1); % design line
\fill [red!50!cyan, above ,opacity=0.3, variable=\x]
(-1,4)
-- plot ({\x}, {7/2 - (1/2)*\x})
-- (5,4)
-- cycle;
\draw[name path=line3] (-1,0) -- node[above left,sloped] {\tiny$x_A\geq0$} (5,0); % design line
\fill [red!50!cyan, above ,opacity=0.3, variable=\x]
(-1,-1)
-- plot ({\x}, {0})
-- (5,-1)
-- cycle;
\draw[name path=line3] (0,-1) -- node[above left,sloped] {\tiny$x_B\geq0$} (0,4); % design line
\fill [red!50!cyan, above ,opacity=0.3, variable=\x]
(-1,-1)
-- plot ({0}, {\x})
-- (-1,4)
-- cycle;
\end{tikzpicture}
\end{document}
欢迎提出任何建议
答案1
这是您图表的实现。请注意,实现它的方法有很多种,其中一些方法比其他方法更有效。
我将仅绘制一个区域来解释我是如何做到的:
我保留了您的网格和轴。我只是给出了 x 轴和 y 轴的名称路径。
我用的是intersections
tikz 库允许计算命名线之间的交点。为此,我需要定义几条线。在下面的代码中,您可以看到我如何定义它们。最重要的部分是
\path [name intersections={of=line1 and line2, by=pt-of-intersection}] (1,0) -- (pt-of-intersection);
代码
\documentclass{article}
\usepackage{tikz,pgfplots}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
% The grid
\draw[gray!50, thin, step=0.5] (-1,-1) grid (5,4);
\draw[name path = axis-x, very thick,->] (-1,0) -- (5,0) node[right] {$x_1$};
\draw[name path = axis-y, very thick,->] (0,-1) -- (0,5) node[above] {$x_2$};
% the axes' ticks
\foreach \x in {-1,...,5} \draw (\x,0.05) -- (\x,-0.05) node[below] {\tiny\x};
\foreach \y in {-2.5,...,4} \draw (-0.05,\y) -- (0.05,\y) node[right] {\tiny\y};
% draw the line and name it line1
\draw[name path=line1] (3,4) -- node[above right,sloped] {\tiny$2x_A+x_B\leq8$} (4.5,-1);
% draw define an asymptote at y=4 but don't draw it
\path [name path = asymptote] (-1,4)--(5,4);
% compute intersection of the two lines named asymptote and line1, the output is named y-cross
\path [name intersections={of=asymptote and line1, by=y-cross}];
% compute intersection of the two lines named axis-x and line1, the output is named x-cross
\path [name intersections={of=axis-x and line1, by=x-cross}];
% fill the spaec between the 4 points
\fill [blue!50!cyan,opacity=0.3](y-cross) -- (x-cross)-- (5,0)-- (5,4);
\end{tikzpicture}
\end{document}
如果你理解了这一点,那么你应该能够自己完成另一件事。这回答了你的问题吗?