TransWikia.com

automating xbar as ybar in pgfplots

TeX - LaTeX Asked by brawbro on March 28, 2021

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”?

One Answer

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] 
   ifnumZ>1
    let p1=(i-thenumexprZ-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] 
   ifnumZ>1
    let p1=(i-thenumexprZ-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] 
     ifnumZ>1
      let p1=(i-thenumexprZ-1relax-1),p2=(i-thenumexprZ-1relax-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] 
     ifnumZ>1
      let p1=(i-thenumexprZ-1relax-1),p2=(i-thenumexprZ-1relax-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}

enter image description here

Answered by user194703 on March 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP