曲线下的阴影区域 TikZ

曲线下的阴影区域 TikZ

使用我从这里,我试图制作一个高度y = 6介于边界x = 2和之间的小矩形x = 7,并将其遮蔽。

知道为什么这不起作用吗?

\documentclass[12pt]{standalone}
\usepackage{pgfplots}
\usepackage{tkz-euclide}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{width=10cm,compat=1.9}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  axis x line=middle, axis y line=middle,
  ymin=0, ymax=10, ytick={0,2,...,10}, ylabel=$f(x)$,
  xmin=0, xmax=10, xtick={0,2,...,10}, xlabel=$x$,
  domain=-pi:pi,samples=101, % added
]
    \addplot[domain=2:7,blue,name path=A] {6}; % actual curve
    \addplot[draw=none,name path=B] {0};     % “fictional” curve
    \addplot[gray] fill between[of=A and B,soft clip={domain=2:7}]; %filling
\addplot+[
      blue,very thick,dotted,
      mark=none,
      const plot,
      empty line=jump,
]
coordinates {
    (2,0)
    (2,6)

    (7,0)
    (7,6)
};
\end{axis}
\end{tikzpicture}

\end{document}

答案1

它不起作用,因为domain=-pi:pi您在轴选项中设置的域用于名为的图B。如果您为该图添加适当的域,它将按预期工作。

\documentclass[12pt]{standalone}
\usepackage{pgfplots}
\usepackage{tkz-euclide}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{width=10cm,compat=1.16}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  axis x line=middle, axis y line=middle,
  ymin=0, ymax=10, ytick={0,2,...,10}, ylabel=$f(x)$,
  xmin=0, xmax=10, xtick={0,2,...,10}, xlabel=$x$,
  domain=-pi:pi,samples=101, % added
]
    \addplot[domain=2:7,blue,name path=A] {6}; % actual curve
    \addplot[draw=none,name path=B,domain=2:7] {0}; 

\addplot[gray] fill between[of=A and B,soft clip={domain=2:7}]; %filling
\addplot+[
      blue,very thick,dotted,
      mark=none,
      const plot,
      empty line=jump,
]
coordinates {
    (2,0)
    (2,6)

    (7,0)
    (7,6)
};
\end{axis}
\end{tikzpicture}
\end{document}  

在此处输入图片描述

附录:只是为了向 Stefan Pinnow 表明他的提议肯定不是最简单的。此代码

\documentclass[12pt]{standalone}
\usepackage{pgfplots}
\usepackage{tkz-euclide}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{width=10cm,compat=1.16}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  axis x line=middle, axis y line=middle,
  ymin=0, ymax=10, ytick={0,2,...,10}, ylabel=$f(x)$,
  xmin=0, xmax=10, xtick={0,2,...,10}, xlabel=$x$,
  domain=-pi:pi,samples=101, % added
]

    \fill[gray] (2,0) rectangle (7,6);

\addplot+[
      blue,very thick,dotted,
      mark=none,
      const plot,
      empty line=jump,
]
coordinates {
    (2,0)
    (2,6)

    (7,0)
    (7,6)
};
\end{axis}
\end{tikzpicture}
\end{document}  

输出结果相同。但这是不是讨论的重点。我认为,重点在于您的代码产生了意想不到的结果(或者在编辑之前被称为“时髦”的结果)。要对矩形进行着色,您甚至不需要 pgfplots,实际上甚至不需要 TiZ。

答案2

正如 marmot 指出的那样他的回答造成混乱的原因是这条道路有错误的。获取所需内容的最简单方法是(仅)在选项中domain设置,然后在其他所有位置将其删除。domainaxis

但要简单地绘制一个矩形,则不需要该fillbetween库。有关详细信息,请查看代码中的注释。

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{fillbetween}
    \pgfplotsset{width=10cm,compat=1.16}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis x line=middle,
        axis y line=middle,
        ymin=0,
        ymax=10,
        ytick={0,2,...,10},
        ylabel=$f(x)$,
        xmin=0,
        xmax=10,
        xtick={0,2,...,10},
        xlabel=$x$,
        domain=2:7, % <-- adapted to the "right" bounds
        samples=2,  % <-- (for straight lines 2 is enough)
    ]

        % alternative without using the `fillbetween' library
        \addplot [fill=red,opacity=0.5] coordinates { (2,6) (7,6) }
            \closedcycle
        ;

        \addplot [blue,name path=A] {6};
        \addplot [draw=none,name path=B] {0};
        \addplot [gray] fill between [
            of=A and B,
%            soft clip={domain=2:7}     % <-- not needed any more now
        ];

        \addplot+ [
              blue,very thick,dotted,
              mark=none,
              const plot,
              empty line=jump,
        ] coordinates {
            (2,0)
            (2,6)

            (7,0)
            (7,6)
        };

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

该图显示了上述代码的结果

相关内容