使用 tikz 对区域进行着色的问题

使用 tikz 对区域进行着色的问题

我在给一个有三个限制的区域上色时遇到了问题。我想上色的区域位于以下点之间:(0,0)(0,1.5)(2,1.5)(2.5,1.25)(2.5,0)(0,0)如您所见,我设法做了一些事情,但无论我尝试什么,大三角形或小三角形都会被着色。我希望你能帮忙。 在此处输入图片描述

\begin{tikzpicture}
 \begin{axis}[
    axis lines = left,
    width=10cm, height=8cm,
    ymin=0,
    xmin=0,
    ymax=3,
    xmax=6,
    xticklabels={,,},
    yticklabels={,,},
    clip=false
]   
\addplot[smooth][domain=0:5, color=black,line width=0.4mm]{1.5}; 
\addplot[smooth][name path=f,domain=0:5, color=black, line width=0.4mm]{(5-x)/2};    

\addplot[smooth] coordinates {(2.5,-0.1) (2.5,3)} [color=black, line width=0.4 mm];
\path[name path=axis](axis cs:0,0) -- (axis cs:2.5,0);
\addplot [
    thick,
    color=brown,
    fill=blue, 
    fill opacity=0.5
]
fill between[
    of=axis and f,
    soft clip={domain=0:2.5},
];

  \node at (axis cs:-0.3,-0.3){$(0,0)$};
  \node at (axis cs:2.5,-0.3){$(2.5,0)$};
  \node at (axis cs:5,-0.3){$(5,0)$};
  \node at (axis cs:-0.6,2.5){$(0,2.5)$};
  \node at (axis cs:1.7,1.3){$(2,1.5)$};
  \node at (axis cs:3.2,1.7){$(2.5,1.5)$};
  \node at (axis cs:3.3,1.25){$(2.5,1.25)$};
  \node at (axis cs:-0.6,1.5){$(0,1.5)$};

\fill (0,0) circle[radius=2pt];
\fill (2.5,0) circle[radius=2pt];
\fill (5,0) circle[radius=2pt];
\fill (0,2.5) circle[radius=2pt];
\fill (2,1.5) circle[radius=2pt];
\fill (2.5,1.5) circle[radius=2pt];
\fill (2.5,1.25) circle[radius=2pt];
\fill (0,1.5) circle[radius=2pt];

\addplot[dashed][domain=-1/2:1/2, color=black, line width=0.4mm]{-x/3};
\end{axis}
\end{tikzpicture}

答案1

一种选择是使用\fill带有边界坐标列表的常规命令。

\fill[red,draw=blue] plot coordinates { (x1,y1)(x2,y2)(x3,y3)(x4,y4) ...)

结果:

在此处输入图片描述

梅威瑟:

\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture}
 \begin{axis}[
    axis lines = left,
    width=10cm, height=8cm,
    ymin=0,
    xmin=0,
    ymax=3,
    xmax=6,
    xticklabels={,,},
    yticklabels={,,},
    clip=false
]   
\addplot[smooth][domain=0:5, color=black,line width=0.4mm]{1.5}; 
\addplot[smooth][name path=f,domain=0:5, color=black, line width=0.4mm]{(5-x)/2};    

\addplot[smooth] coordinates {(2.5,-0.1) (2.5,3)} [color=black, line width=0.4 mm];
\path[name path=axis](axis cs:0,0) -- (axis cs:2.5,0);

\fill[red,draw=blue] plot coordinates {
  (axis cs:0,0)
  (axis cs:0,1.5)
  (axis cs:2,1.5)
  (axis cs:2.5,1.25)
  (axis cs:2.5,0)
  (axis cs:0,0)
} ;
\node at (axis cs:-0.3,-0.3){$(0,0)$};
\node at (axis cs:2.5,-0.3){$(2.5,0)$};
\node at (axis cs:5,-0.3){$(5,0)$};
\node at (axis cs:-0.6,2.5){$(0,2.5)$};
\node at (axis cs:1.7,1.3){$(2,1.5)$};
\node at (axis cs:3.2,1.7){$(2.5,1.5)$};
\node at (axis cs:3.3,1.25){$(2.5,1.25)$};
\node at (axis cs:-0.6,1.5){$(0,1.5)$};

\fill (axis cs:0,0) circle[radius=2pt];
\fill (axis cs:2.5,0) circle[radius=2pt];
\fill (axis cs:5,0) circle[radius=2pt];
\fill (axis cs:0,2.5) circle[radius=2pt];
\fill (axis cs:2,1.5) circle[radius=2pt];
\fill (axis cs:2.5,1.5) circle[radius=2pt];
\fill (axis cs:2.5,1.25) circle[radius=2pt];
\fill (axis cs:0,1.5) circle[radius=2pt];

\addplot[dashed][domain=-1/2:1/2, color=black, line width=0.4mm]{-x/3};

\end{axis} \end{tikzpicture}
\end{document}

答案2

根据我在这里的回答: https://tex.stackexchange.com/a/398639/120578

您必须对第一个填充和第二个填充使用单独的命令,因为第一个是命名A图(添加名称......参见注释)直到x=2然后fx=2直到x=2.5

\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{intersections}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
 \begin{axis}[
    axis lines = left,
    width=10cm, height=8cm,
    ymin=0,
    xmin=0,
    ymax=3,
    xmax=6,
    xticklabels={,,},
    yticklabels={,,},
    clip=false
]   
\addplot[smooth][domain=0:5, color=black,line width=0.4mm,name path=A]{1.5};%Added name A 
\addplot[smooth][name path=f,domain=0:5, color=black, line width=0.4mm]{(5-x)/2};    

\addplot[smooth] coordinates {(2.5,-0.1) (2.5,3)} [color=black, line width=0.4 mm];
\path[name path=axis](axis cs:0,0) -- (axis cs:2.5,0);
%\addplot [% Replaced with below
%    thick,
%    color=brown,
%    fill=blue, 
%    fill opacity=0.5
%]
%fill between[
%    of=axis and f,
%    soft clip={domain=0:2.5},
%];
\addplot[thick,
  color=brown,
  fill=blue,
  fill opacity=0.5
] fill between[of=axis and A,soft clip={domain=0:2}];
\addplot[thick,
  color=brown,
  fill=blue,
  fill opacity=0.5
] fill between[of=axis and f,soft clip={domain=2:2.5}];
  \node at (axis cs:-0.3,-0.3){$(0,0)$};
  \node at (axis cs:2.5,-0.3){$(2.5,0)$};
  \node at (axis cs:5,-0.3){$(5,0)$};
  \node at (axis cs:-0.6,2.5){$(0,2.5)$};
  \node at (axis cs:1.7,1.3){$(2,1.5)$};
  \node at (axis cs:3.2,1.7){$(2.5,1.5)$};
  \node at (axis cs:3.3,1.25){$(2.5,1.25)$};
  \node at (axis cs:-0.6,1.5){$(0,1.5)$};

\fill (0,0) circle[radius=2pt];
\fill (2.5,0) circle[radius=2pt];
\fill (5,0) circle[radius=2pt];
\fill (0,2.5) circle[radius=2pt];
\fill (2,1.5) circle[radius=2pt];
\fill (2.5,1.5) circle[radius=2pt];
\fill (2.5,1.25) circle[radius=2pt];
\fill (0,1.5) circle[radius=2pt];

\addplot[dashed][domain=-1/2:1/2, color=black, line width=0.4mm]{-x/3};
\end{axis} \end{tikzpicture}
\end{document}

但是在你的情况下如果你知道坐标,那么@Bobyandbod 的评论和回答的方式就更简单了:

输出:

在此处输入图片描述

相关内容