# automating xbar as ybar in pgfplots

I am endeavoring to make a demonstration of the conceptual difference between the Lebesgue V Riemann integrals. It's easy to automate the ybar command to give left right or midpoint sums as seen at Placing coloured rectangles on a plot, using points from the plot - Riemann Sums.

I could make the boxes by hand a couple of different ways, but I would like to avoid brute force if possible. The main trouble is that xbar always wants to go back to xmin (which again is an obvious means of one-at-a-timing it) Any ideas on how to automate the xbar command as with the ybar above to make sideways "Lebesgue Sums"?

• alternatively, is there a method of say, stacking "squished" ybars? Commented Mar 10, 2020 at 21:02
• It would be great if you could tell us how precisely the diagram should look like, and even better if you could show us what you have tried.
– user194703
Commented Mar 10, 2020 at 22:06
– user194703
Commented Mar 11, 2020 at 0:38
• en.wikipedia.org/wiki/Lebesgue_integration#/media/… i am trying to generate the bottom image, but without having to code each individual rectangle Commented Mar 11, 2020 at 0:48

Like in this answer, you can use intersections here.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{backgrounds,calc,intersections}
\begin{document}
\begin{tikzpicture}
\draw[thick,name path=A] plot[smooth] coordinates {(0,0) (1,0.8) (2,1) (3.5,0.6) (4,0)};
\begin{scope}[on background layer]
\foreach \X [count=\Z] in {0,0.5,...,4}
{\path[name path global=v-\Z,overlay] (\X,0) --  (\X,0|-current bounding box.north);
\draw[name intersections={of=A and v-\Z,by=i-\Z},blue,fill=blue!20]
\ifnum\Z>1
let \p1=(i-\the\numexpr\Z-1),\p2=(i-\Z) in
(\X-0.5,0) rectangle (\X,{max(\y1,\y2)})
\fi;
\draw[name intersections={of=A and v-\Z,by=i-\Z},blue,fill=blue!80]
\ifnum\Z>1
let \p1=(i-\the\numexpr\Z-1),\p2=(i-\Z) in
(\X-0.5,0) rectangle (\X,{min(\y1,\y2)})
\fi;}
\end{scope}
%
\begin{scope}[yshift=-2cm]
\draw[thick,name path=B] plot[smooth] coordinates {(0,0) (1,0.8) (2,1) (3.5,0.6) (4,0)};
\begin{scope}[on background layer]
\foreach \Y [count=\Z] in {0,0.2,0.4,0.6,0.8,1}
{\path[name path global=h-\Z,overlay] (0,\Y) --  (4,\Y);
\draw[name intersections={of=B and h-\Z,by={i-\Z-1,i-\Z-2}},red,fill=red!20]
\ifnum\Z>1
let \p1=(i-\the\numexpr\Z-1\relax-1),\p2=(i-\the\numexpr\Z-1\relax-2),
\p3=(i-\Z-1),\p4=(i-\Z-2)
in
({min(\x1,\x3)},\Y-0.2) rectangle ({max(\x2,\x4)},\Y)
\fi;
\draw[name intersections={of=B and h-\Z,by={i-\Z-1,i-\Z-2}},red,fill=red!60]
\ifnum\Z>1
let \p1=(i-\the\numexpr\Z-1\relax-1),\p2=(i-\the\numexpr\Z-1\relax-2),
\p3=(i-\Z-1),\p4=(i-\Z-2)
in
({max(\x1,\x3)},\Y-0.2) rectangle ({min(\x2,\x4)},\Y)
\fi;
}
\end{scope}
\end{scope}
\end{tikzpicture}
\end{document}