![带颜色图/热图的三维条形图](https://linux22.com/image/453153/%E5%B8%A6%E9%A2%9C%E8%89%B2%E5%9B%BE%2F%E7%83%AD%E5%9B%BE%E7%9A%84%E4%B8%89%E7%BB%B4%E6%9D%A1%E5%BD%A2%E5%9B%BE.png)
我正在尝试绘制一个 3d 条形图,其中条形图用颜色图/热图着色。我刚刚找到了这个解决方案:在此处输入链接描述获得 3D 条形图,但我没有获得每个条形图的颜色图的解决方案。
最后我想制作一个这样的情节:
我可以用这个简单的 matlab 代码得到:
test_table = [5,3,2,1,2,0.5,3; 4,5,3,1,2,2,0.5; 4,6,2,4,3,2,1];
figure(1)
xy = bar3(test_table);
colorbar
for z = 1:length(xy)
xy(z).CData = xy(z).ZData;
xy(z).FaceColor = 'interp';
end
有人知道如何使用颜色图为 3d 条着色吗?非常感谢!
答案1
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{patchplots}
\begin{document}
\begin{tikzpicture}
\newcommand{\barw}{0.4}
\newcommand{\boxbar}[3]{
\addplot3[
patch,
shader=interp,
patch type=rectangle,
patch refines=2,
]
coordinates { (#1-\barw,#2-\barw,0) (#1-\barw,#2-\barw,#3) (#1+\barw,#2-\barw,#3) (#1+\barw,#2-\barw,0) (#1-\barw,#2-\barw,#3) (#1-\barw,#2+\barw,#3) (#1+\barw,#2+\barw,#3) (#1+\barw,#2-\barw,#3) (#1+\barw,#2-\barw,0) (#1+\barw,#2-\barw,#3) (#1+\barw,#2+\barw,#3) (#1+\barw,#2+\barw,0)};
\addplot3[
patch,
patch type=rectangle,
mesh, black, very thin,
]
coordinates { (#1-\barw,#2-\barw,0) (#1-\barw,#2-\barw,#3) (#1+\barw,#2-\barw,#3) (#1+\barw,#2-\barw,0) (#1-\barw,#2-\barw,#3) (#1-\barw,#2+\barw,#3) (#1+\barw,#2+\barw,#3) (#1+\barw,#2-\barw,#3) (#1+\barw,#2-\barw,0) (#1+\barw,#2-\barw,#3) (#1+\barw,#2+\barw,#3) (#1+\barw,#2+\barw,0)};
}
\begin{axis}[
colormap={CM}{
color=(blue!50!black)
color=(blue!50!white)
color=(cyan!70!white)
color=(green!50!yellow)
color=(orange!50!yellow)
color=(yellow)
},
view={60}{30},
unit vector ratio=1.2 1 0.6,
xmin=0.4, xmax=3.6,
ymin=0.6, ymax=7.6,
zmin=0, zmax=6,
colorbar, colorbar/width=6pt,
xtick distance=1, ytick distance=1, ztick distance=1,
font=\tiny,
]
\foreach \myz [count=\i] in {3,0.5,2,1,2,3,5} {
\boxbar{1}{8-\i}{\myz}}
\foreach \myz [count=\i] in {0.5,2,2,1,3,5,4}{
\boxbar{2}{8-\i}{\myz}}
\foreach \myz [count=\i] in {1,2,3,4,2,6,4}{
\boxbar{3}{8-\i}{\myz}}
\end{axis}
\end{tikzpicture}
\end{document}
使用此处的 parula 颜色图:https://tex.stackexchange.com/a/350158/8650
答案2
此外赫佩克里斯蒂安森很好的答案,我根据他的例子尝试了一些进一步的步骤,我想分享我的解决方案,即从外部文件生成带有颜色图的 3d 条形图:
和
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{patchplots}
% define a colormap named "CM"
\pgfplotsset{colormap={CM}{
color=(blue!50!black)
color=(blue!50!white)
color=(cyan!70!white)
color=(green!50!yellow)
color=(orange!50!yellow)
color=(yellow)
}}
%Producing an example .csv file with same values
\begin{filecontents*}{Bar_Test_file.csv}
5,3,2,1,2,0.5,3
4,5,3,1,2,2,0.5
4,6,2,4,3,2,1
\end{filecontents*}
%Import file to save into variable "mytable for further processing
\pgfplotstableread[header=false, col sep = comma]{Bar_Test_file.csv}{\mytable}
\pgfplotstablegetrowsof{\mytable} %get number of rows
\pgfmathsetmacro{\rows}{\pgfplotsretval-1} %last "row" is number of rows minus 1 (0,1...)
\pgfplotstablegetcolsof{\mytable} %get number of cols
\pgfmathsetmacro{\cols}{\pgfplotsretval-1} %last "col" is number of cols minus 1 (0,1...)
\begin{document}
\begin{tikzpicture}
\newcommand{\barw}{0.4} %barwidth
\newcommand{\barl}{0.4} %barlength
%define a "boxbar"
\newcommand{\boxbar}[3]{
%% Coloring the bar
\addplot3[
patch,
shader=interp,
patch type=rectangle,
patch refines=2,
]
coordinates { (#1-\barw,#2-\barl,0) (#1-\barw,#2-\barl,#3) (#1+\barw,#2-\barl,#3) (#1+\barw,#2-\barl,0) (#1-\barw,#2-\barl,#3) (#1-\barw,#2+\barl,#3) (#1+\barw,#2+\barl,#3) (#1+\barw,#2-\barl,#3) (#1+\barw,#2-\barl,0) (#1+\barw,#2-\barl,#3) (#1+\barw,#2+\barl,#3) (#1+\barw,#2+\barl,0)};
%% Borders of a bar
\addplot3[
patch,
patch type=rectangle,
mesh, black, very thin,
]
coordinates { (#1-\barw,#2-\barl,0) (#1-\barw,#2-\barl,#3) (#1+\barw,#2-\barl,#3) (#1+\barw,#2-\barl,0) (#1-\barw,#2-\barl,#3) (#1-\barw,#2+\barl,#3) (#1+\barw,#2+\barl,#3) (#1+\barw,#2-\barl,#3) (#1+\barw,#2-\barl,0) (#1+\barw,#2-\barl,#3) (#1+\barw,#2+\barl,#3) (#1+\barw,#2+\barl,0)};
}
\begin{axis}[
colormap name = CM,
view={60}{30},
unit vector ratio=1.2 1 0.6,
xmin=0.4, xmax=3.6,
ymin=0.6, ymax=7.6,
zmin=0, zmax=6,
colorbar, colorbar/width=6pt,
xtick distance=1, ytick distance=1, ztick distance=1,
font=\tiny,
]
\foreach \j in {0,...,\rows}{%
\foreach \i in {0,...,\cols}{%
\pgfplotstablegetelem{\j}{[index] \i}\of{\mytable}
\let\myz\pgfplotsretval \boxbar{\j+1}{\cols+1-\i}{\myz}}}
\end{axis}
\end{tikzpicture}
\end{document}
可以从 .csv 文件创建相同的图。我希望这将有助于某人制作未来的 3D 条形图。