1

Using code I got from here, I'm trying to make a small rectangle of height y = 6 between bounds x = 2 and x = 7, and shade it in.

Any idea why this doesn't work?

\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

It does not work because the domain domain=-pi:pi, which you set in the axis options, is used for the plot named B. If you add the appropriate domain for that plot, it works as expected.

\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}  

enter image description here

ADDENDUM: Just to show Stefan Pinnow that his proposal is certainly not the simplest one. This code

\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}  

yields the same output. But this is not the point of the discussion. The point, I think, is that your code yields an unexpected (or "funky" as they were called before the edit) results. To shade a rectangle, you do not need even pgfplots, actually not even TikZ.

|improve this answer|||||
1

As marmot already pointed out in his answer what causes the mess is that path B has the wrong domain. The simplest way to get what you want is to set the domain in the axis options (only) and remove it everywhere else.

But to simply draw a rectangle you don't need the fillbetween library. For details please have a look at the comments in the code.

% 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}

image showing the result of above code

|improve this answer|||||
  • Well, sure, you do not need pgfplots to draw as square. However, my impression was that the OP was kind enough to cut out everything that is not relevant to the problem, and will in the end make use of the domain=-pi:pi and samples for the things that do require pgfplots. In that case, the changes you made may not be too helpful. Of course, if the OP only wants to draw a square, (s)he will be fine with that, but this can be done with a simple \fill command, too. – user121799 Oct 4 '18 at 20:15
  • Sure, there are plenty of ways to draw a (simple) rectangle. And that is exactly what OP was searching for. The main reason for my answer was to show an alternative to the fillbetween which is kind of overkill for that simple task. And as you can see, I have drawn this without using domain. And of course also \fill [gray] (axis:cs:2,0) rectangle (axis cs:7,6); would have been another possibility to achieve the desired result. – Stefan Pinnow Oct 4 '18 at 20:21
  • Yes, that's why the question is not "How can one fill a square" but "Shading area under curve TikZ".... – user121799 Oct 4 '18 at 20:27
  • That's the title, but in the text ... – Stefan Pinnow Oct 4 '18 at 20:29
  • Well, this can go on forever. How about someone adds a 3-line code that just fills a square and says both of us have missed that simple possibility? Again, my take is that the OP uses pgfplots and sets the domain and samples for a reason.... – user121799 Oct 4 '18 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.