# Adding the surface to a Riemann Sum

The @user240002 in the post in Riemman Sum built a Riemann Sum of a two variables function:

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\tikzset{pics/3d bar/.style={code={%
\tikzset{3d bar/.cd,#1}
\path[3d bar/x face] (\mydx/2,\mydy/2,0) -- (\mydx/2,\mydy/2,\myh)
-- (-\mydx/2,\mydy/2,\myh) -- (-\mydx/2,\mydy/2,0) -- cycle;
\path[3d bar/y face] (\mydx/2,\mydy/2,0) -- (\mydx/2,\mydy/2,\myh)
-- (\mydx/2,-\mydy/2,\myh) -- (\mydx/2,-\mydy/2,0) -- cycle;
\path[3d bar/z face] (\mydx/2,\mydy/2,\myh) -- (-\mydx/2,\mydy/2,\myh)
-- (-\mydx/2,-\mydy/2,\myh) -- (\mydx/2,-\mydy/2,\myh) -- cycle;
}},3d bar/.cd,dx/.store in=\mydx,dx=1,dy/.store in=\mydy,dy=1,
h/.store in=\myh,h=1,x face/.style={draw=blue!50,fill=cyan!20},
y face/.style={draw=blue!50,fill=cyan!50},
z face/.style={draw=blue!50,fill=cyan!30}}
\begin{document}
\tdplotsetmaincoords{70}{110}%
\begin{tikzpicture}[tdplot_main_coords]
\begin{scope}[declare function={f(\x,\y)=1+3*exp(-\x/5-\y/4);% function
n=5;% steps
xmin=0;xmax=5;ymin=0;ymax=5;}]
\pgfmathtruncatemacro{\myn}{n}
\tikzset{3d bar/dx/.evaluated={(xmax-xmin)/n},
3d bar/dy/.evaluated={(ymax-ymin)/n}}
\foreach \i in {1,...,\myn}
{\foreach \j in {1,...,\myn}
{\pgfmathsetmacro{\myx}{xmin+(\i-0.5)*(xmax-xmin)/\myn}
\pgfmathsetmacro{\myy}{ymin+(\j-0.5)*(ymax-ymin)/\myn}
\pgfmathsetmacro{\myf}{f(\myx,\myy)}
\path (\myx,\myy,0) pic{3d bar={h=\myf}};}}
\end{scope}
%
\begin{scope}[xshift=7cm,
declare function={f(\x,\y)=0.5+3*exp(-\x/5-\y/4);% function
n=20;% steps
xmin=0;xmax=5;ymin=0;ymax=5;}]
\pgfmathtruncatemacro{\myn}{n}
\tikzset{3d bar/dx/.evaluated={(xmax-xmin)/n},
3d bar/dy/.evaluated={(ymax-ymin)/n}}
\foreach \i in {1,...,\myn}
{\foreach \j in {1,...,\myn}
{\pgfmathsetmacro{\myx}{xmin+(\i-0.5)*(xmax-xmin)/\myn}
\pgfmathsetmacro{\myy}{ymin+(\j-0.5)*(ymax-ymin)/\myn}
\pgfmathsetmacro{\myf}{f(\myx,\myy)}
\path (\myx,\myy,0) pic{3d bar={h=\myf}};}}
\end{scope}
\end{tikzpicture}
\end{document}


How can I add the graph of surface z=f(x,y), and axis together?

• I obtain a Package pgfkeys Error: I do not know the key '/tikz/3d bar/dx/.evaluated' upon compilation. Apr 18, 2021 at 0:10
• @Karlo The /.evaluated key exists only in newer pgf versions. Probably you are using an older one.
– user240002
Apr 18, 2021 at 5:07
• Angelo, you have it because it was written by @user240002 in tex.stackexchange.com/a/593395/38080. Can I humbly suggest to give attribution with a link when using code from others here? Apr 18, 2021 at 7:03
• Yes sure! The orriginal code was proposed by @user240002 in: tex.stackexchange.com/questions/593371/… Apr 18, 2021 at 12:12

It is not entirely clear to me what you want but perhaps this goes in the right direction. You can reprogram the scatter code to draw the stair-like plot.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17,
stair plot/.code={\pgfplotsset{stairs/.cd,#1}%
\def\myvalue##1{\pgfkeysvalueof{/pgfplots/stairs/##1}}%
\pgfmathsetmacro{\mydx}{(\myvalue{xmax}-\myvalue{xmin})/(\myvalue{n})}%
\pgfmathsetmacro{\mydy}{(\myvalue{ymax}-\myvalue{ymin})/(\myvalue{m})}%
\pgfmathsetmacro{\myxmin}{\myvalue{xmin}+\mydx/2}%
\pgfmathsetmacro{\myymin}{\myvalue{ymin}+\mydy/2}%
\pgfmathsetmacro{\myxmax}{\myvalue{xmax}-\mydx/2}%
\pgfmathsetmacro{\myymax}{\myvalue{ymax}-\mydy/2}%
\pgfplotsset{domain=\myxmin:\myxmax,domain y=\myymin:\myxmax,
samples=\myvalue{n},samples y=\myvalue{m},
scatter,only marks,mark=none,
scatter/@pre marker code/.append code={%
\pgfmathsetmacro{\myx}{\pgfkeysvalueof{/data point/x}}%
\pgfmathsetmacro{\myy}{\pgfkeysvalueof{/data point/y}}%
\pgfmathsetmacro{\myz}{\pgfkeysvalueof{/data point/z}}%
\path[3d bar/x face] (axis direction cs:\mydx/2,\mydy/2,0) --
(axis direction cs:\mydx/2,\mydy/2,-1*\myz) --
(axis direction cs:-\mydx/2,\mydy/2,-1*\myz) --
(axis direction cs:-\mydx/2,\mydy/2,0) -- cycle;
\path[3d bar/y face] (axis direction cs:\mydx/2,\mydy/2,0) --
(axis direction cs:\mydx/2,\mydy/2,-1*\myz) --
(axis direction cs:\mydx/2,-\mydy/2,-1*\myz) --
(axis direction cs:\mydx/2,-\mydy/2,0) -- cycle;
\path[3d bar/z face] (axis direction cs:\mydx/2,\mydy/2,0) --
(axis direction cs:\mydx/2,-\mydy/2,0) --
(axis direction cs:-\mydx/2,-\mydy/2,0) --
(axis direction cs:-\mydx/2,\mydy/2,0) -- cycle;
},
}%
},
stairs/.cd,
xmin/.initial=0,xmax/.initial=5,ymin/.initial=0,ymax/.initial=5,
n/.initial=5,m/.initial=5}
\tikzset{3d bar/.cd,
x face/.style={draw=blue!50,fill=cyan!30},
y face/.style={draw=blue!50,fill=cyan!50},
z face/.style={draw=blue!50,fill=cyan!10}}

\begin{document}
\begin{tikzpicture}[declare function={f(\x,\y)=3*exp(-\x/5-\y/4);},% function
/pgfplots/.cd,view={120}{40},
xmin=0,xmax=6,
ymin=0,ymax=6,
zmin=0
]
\begin{axis}
\end{axis}
\begin{axis}[yshift=6cm]
\end{axis}
\begin{axis}[xshift=8cm]
\end{axis}
\begin{axis}[xshift=8cm,yshift=6cm]
\end{axis}
\end{tikzpicture}
\end{document}


Or

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17,
stair plot/.code={\pgfplotsset{stairs/.cd,#1}%
\def\myvalue##1{\pgfkeysvalueof{/pgfplots/stairs/##1}}%
\pgfmathsetmacro{\mydx}{(\myvalue{xmax}-\myvalue{xmin})/(\myvalue{n})}%
\pgfmathsetmacro{\mydy}{(\myvalue{ymax}-\myvalue{ymin})/(\myvalue{m})}%
\pgfmathsetmacro{\myxmin}{\myvalue{xmin}+\mydx/2}%
\pgfmathsetmacro{\myymin}{\myvalue{ymin}+\mydy/2}%
\pgfmathsetmacro{\myxmax}{\myvalue{xmax}-\mydx/2}%
\pgfmathsetmacro{\myymax}{\myvalue{ymax}-\mydy/2}%
\pgfplotsset{domain=\myxmin:\myxmax,domain y=\myymin:\myxmax,
samples=\myvalue{n},samples y=\myvalue{m},
scatter,only marks,mark=none,
scatter/@pre marker code/.append code={%
\pgfmathsetmacro{\myx}{\pgfkeysvalueof{/data point/x}}%
\pgfmathsetmacro{\myy}{\pgfkeysvalueof{/data point/y}}%
\pgfmathsetmacro{\myz}{\pgfkeysvalueof{/data point/z}}%
\path[3d bar/x face] (axis direction cs:\mydx/2,\mydy/2,0) --
(axis direction cs:\mydx/2,\mydy/2,-1*\myz) --
(axis direction cs:-\mydx/2,\mydy/2,-1*\myz) --
(axis direction cs:-\mydx/2,\mydy/2,0) -- cycle;
\path[3d bar/y face] (axis direction cs:\mydx/2,\mydy/2,0) --
(axis direction cs:\mydx/2,\mydy/2,-1*\myz) --
(axis direction cs:\mydx/2,-\mydy/2,-1*\myz) --
(axis direction cs:\mydx/2,-\mydy/2,0) -- cycle;
\path[3d bar/z face] (axis direction cs:\mydx/2,\mydy/2,0) --
(axis direction cs:\mydx/2,-\mydy/2,0) --
(axis direction cs:-\mydx/2,-\mydy/2,0) --
(axis direction cs:-\mydx/2,\mydy/2,0) -- cycle;
},
}%
},
stairs/.cd,
xmin/.initial=0,xmax/.initial=5,ymin/.initial=0,ymax/.initial=5,
n/.initial=5,m/.initial=5}
\tikzset{3d bar/.cd,
x face/.style={draw=blue!50,fill=cyan!30},
y face/.style={draw=blue!50,fill=cyan!50},
z face/.style={draw=blue!50,fill=cyan!10}}

\begin{document}
\begin{tikzpicture}[declare function={f(\x,\y)=3*exp(-\x/5-\y/4);},% function
/pgfplots/.cd,view={120}{40},
xmin=0,xmax=6,
ymin=0,ymax=6,
zmin=0,xtick=\empty,ytick=\empty,ztick=\empty,
xlabel={$x$},ylabel={$y$},zlabel={$z$}
]
\begin{axis}
\path (0,4,2) node{$n=14$};