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}
Best Answer
It does not work because the domain
domain=-pi:pi
, which you set in the axis options, is used for the plot namedB
. If you add the appropriate domain for that plot, it works as expected.ADDENDUM: Just to show Stefan Pinnow that his proposal is certainly not the simplest one. This code
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.