对图形的不同区域进行着色(tikz)

对图形的不同区域进行着色(tikz)

我想用不同的颜色为图形中由曲线界定的 6 个不同区域着色。似乎我一次只能为一个区域着色。这适用于为一个区域着色:

    \begin{document}
    \begin{tikzpicture}
  \begin{axis}[
    axis y line = left,
    axis x line = bottom,
    xtick       = {5,7},
    xticklabels = {$\hat{z}_1$,$\hat{z}_2$},
    ytick       = {4},
    yticklabels = {$\hat{b}_w$},
    samples     = 160,
    domain      = 0:10,
    xmin = 0, xmax = 10,
    ymin = 0, ymax = 10,
  ]
  % path of axis
  \path[name path=xaxis] (\pgfkeysvalueof{/pgfplots/xmin}, 0) -- (\pgfkeysvalueof{/pgfplots/xmax},0);
  % Plot 1
  \addplot[name path=A, black, no markers, domain=0:5] {4};
  % FILL
  \addplot[blue] fill between[of=A and xaxis, soft clip={domain=0:5}];
    \addplot[name path=B thick,dashed,black] coordinates {(5, 0) (5, 10)};
   \addplot[name path=C thick,dashed,black] coordinates {(7, 0) (7, 10)};
   \addplot[name path=D thick,black] coordinates {(5,4) (7, 5)};
   \end{axis}
\end{tikzpicture}
    \end{document}

但是如果我添加另一行来填充,例如:

\addplot[red] fill between[of=D and xaxis, soft clip={domain=5:7}];

我收到错误:非法测量单位,图表无法编译。有什么解决办法吗?

答案1

由于您没有在代码片段中包含前言,因此您的代码不是真正的最小工作示例。因此,您关于非法计量单位的错误是与您的问题无关fillbetween,这肯定是由于您的文档中的其他内容导致的。

以下代码编译时没有错误,并提供了您想要的效果:两个填充区域,其边界由两条曲线定义。代码中的错误是您没有在后面添加逗号,name path=D这意味着 PGFPlots 认为路径的名称是D thick而不仅仅是D

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    axis y line = left,
    axis x line = bottom,
    xtick       = {5,7},
    xticklabels = {$\hat{z}_1$,$\hat{z}_2$},
    ytick       = {4},
    yticklabels = {$\hat{b}_w$},
    samples     = 160,
    domain      = 0:10,
    xmin = 0, xmax = 10,
    ymin = 0, ymax = 10,
  ]
  % path of axis
  \path[name path=xaxis] (\pgfkeysvalueof{/pgfplots/xmin}, 0) -- (\pgfkeysvalueof{/pgfplots/xmax},0);
  % Plot 1
  \addplot[name path=A, black, no markers, domain=0:5] {4};
  \addplot[name path=B, thick,dashed,black] coordinates {(5, 0) (5, 10)};
  \addplot[name path=C, thick,dashed,black] coordinates {(7, 0) (7, 10)};
  \addplot[name path=D, thick,black] coordinates {(5,4) (7, 5)};
  % FILL
  \addplot[blue] fill between[of=A and xaxis, soft clip={domain=0:5}];
  \addplot[red]  fill between[of=D and xaxis, soft clip={domain=5:7}];
\end{axis}
\end{tikzpicture}
\end{document}

红色区域和蓝色区域。红色区域以曲线 A 和 x 轴为界,蓝色区域以曲线 D 和 x 轴为界。

相关内容