答案1
可能最好使用cube*
中的绘图标记pgfplots
,但对于足够简单的函数,以下方法也可以。您可以定义一个函数、步骤数和绘图范围。
\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}
答案2
如果我可以在 user240002 的精彩回答上加一点小樱桃(一位猫大师曾经教过我)...
为了避免使用(在我看来很烦人\pgfmathsetmacro
),请直接在循环中包含要评估的变量。
\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}
{% here
\foreach [evaluate ={
\myx = xmin+(\i-0.5)*(xmax-xmin)/\myn ;
\myy = ymin+(\j-0.5)*(ymax-ymin)/\myn ;
\myf = f(\myx,\myy) ;
}] \j in {0,...,\myn}
{\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}
{% here
\foreach [evaluate ={
\myx = xmin+(\i-0.5)*(xmax-xmin)/\myn ;
\myy = ymin+(\j-0.5)*(ymax-ymin)/\myn ;
\myf = f(\myx,\myy) ;
}] \j in {0,...,\myn}
{\path (\myx,\myy,0) pic{3d bar={h=\myf}};}
}
\end{scope}
\end{tikzpicture}
\end{document}
更多详细信息请参阅 pfg 手册 v3.1.5.b p1003,第 89 节重复事物:Foreach 语句。