I don't know how to do this with tikz, but since you got no answers so far, I give you a start on how you can do something similar in MetaFun. This might give you an idea on how to proceed with tikz. The path fun is your function, N is the number of colors.
\startMPpage[offset=1dk]
numeric u ; u := 1cm ;
path fun ; fun := ((0.2,-0.2){right} ..
(3.5,5.5){right} ..
(6,2.3){right} ..
(9,3.1){right} ..
{dir -30}(12,-0.25)) ;
numeric miny ; miny := 0 ;
numeric maxy ; maxy := ypart urcorner boundingbox fun ;
numeric N ; N := 8 ;
numeric level[] ;
rgbcolor levelcolor[] ;
path ip[] ;
path tmppath ;
for i = 1 upto N :
level[i] := miny + (maxy - miny)*i/N ;
message(level[i]) ;
% levelcolor[i] := (i/N)[red,yellow] ;
levelcolor[i] := ( uniformdeviate(1), uniformdeviate(1), uniformdeviate(1) ) ;
ip[i] := fun firstintersectionpath ((-infinity,level[i]) -- (infinity,level[i])) ;
endfor
for i = 1 upto N :
for j = 0 step 2 until (length(ip[i]) - 1) :
tmppath := ( (point j of ip[i]) --
(point (j + 1) of ip[i]) --
(xpart point (j + 1) of ip[i], 0) --
(xpart point j of ip[i], 0) --
cycle ) ;
% unfill tmppath ; % no gain
fill tmppath scaled u withcolor levelcolor[i] ;
endfor
if i < N :
draw ((0,level[i]) -- (0, level[i+1])) scaled u withpen pencircle scaled 2 withlinecap butt withcolor levelcolor[i] ;
fi
endfor ;
draw fun scaled u ;
drawdoublearrow ( (0,6) -- origin -- (13,0) ) scaled u ;
\stopMPpage
Save the file as lebesgue.tex and run with context lebesgue.tex to get lebesgue.pdf. With N = 8 we get

The colors are random. With N = 25 we instead get

Finally, by altering the definitions of levelcolor[i], we end up with
