[ad_1]
I want to fill both the area below and above a function in different shades of gray. I thought it would be easier to first fill a rectangle in the lighter gray, then draw the function on top of it and finally fill the region above with the darker gray. However, the last part doesn’t seem to be working, as I get

I believe the problem is that the lighter rectangle gets drawn on top of the darker area. I thought elements in the figure were stacked in the same order as the corresponding instructions, but somehow the darker fill gets to be the first layer and then the rectangle and lastly the function are printed. My code follows
\documentclass[a4paper]{book}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.17}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{figure}
\centering
\footnotesize
\begin{tikzpicture}
\begin{axis}[
ticks=none,
axis x line=bottom,
axis y line=left,
axis on top,
xmin=0,xmax=10,
ymin=0,ymax=10]
\fill[gray!25] (0,0) rectangle (10,10);
\path[name path = x] { (1.6,10) -- (10,10) };
\addplot[
name path=c,
black,
samples=200,
smooth,
thick,
domain=1.6:10]
{16/x};
\addplot[gray!50] fill between [of = c and x];
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
[ad_2]
Source link