在 PGF 中绘制 3D 条形图

在 PGF 中绘制 3D 条形图

我不确定这个问题是否以前被问过,但我想制作像这个例子一样的 3D 图。

在此处输入图片描述

目前,我已经

\documentclass[tikz,border=3.14pt]{standalone}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}[scale=1, transform shape]
\begin{axis}[
view={120}{40},
width=10cm,
height=10cm,
grid=major,
xmin=1,xmax=4,
ymin=1,ymax=5,
zmin=-1,zmax=3,
xtick={1,2,3,4},
xticklabels={L1, L2, L3, L4},
ytick={1,2,3,4,5},
yticklabels={A, B, C, D, E},
ylabel={$y$},
xlabel={$x$},
zlabel={$z$},
axis equal,
]

\addplot3[only marks,scatter,mark=cube*,mark size=1cm, fill=blue]
coordinates {(1,1,0.7)(1,2,0.5)(1,3,0.9)(1,4,0.4)(1,5,0.33)};

\addplot3[only marks,scatter,mark=cube*,mark size=1cm, fill=orange]
coordinates {(2,1,0.6)(2,2,0.3)(2,3,0.65)(2,4,0.67)(2,5,0.31)};

\addplot3[only marks,scatter,mark=cube*,mark size=1cm,fill=violet]
coordinates {(3,1,0.3)(3,2,0.45)(3,3,0.7)(3,4,0.89)(3,5,0.45)};

\addplot3[only marks,scatter,mark=cube*,mark size=1cm,fill=red]
coordinates {(4,1,1)(4,2,0.2)(4,3,0.6)(4,4,0.72)(4,5,0.78)};

\end{axis}
\end{tikzpicture}
\end{document} 

但我无法让条形图从 z=0 开始,因为它们的大小是固定的。在此先感谢所有提供帮助的人,如果我有点愚蠢,请原谅。

答案1

这重复一个老答案在情节标记方面略有新的倾向half cube*

\documentclass[tikz,border=3.14pt]{standalone}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\pgfkeys{/tikz/.cd,
cube top color/.store in=\CubeTopColor,
cube top color=blue!60,
cube front color/.store in=\CubeFrontColor,
cube front color=blue!30,
cube side color/.store in=\CubeSideColor,
cube side color=blue!40,
3d cube color/.code={\colorlet{mycolor}{#1}%
\tikzset{cube top color=mycolor!60,cube front color=mycolor!30,%
cube side color=mycolor!40,draw=mycolor}}
}
\makeatletter
\pgfdeclareplotmark{half cube*}
                {%
                        \pgfplots@cube@gethalf@x
                        \let\pgfplots@cube@halfx=\pgfmathresult
                        \pgfplots@cube@gethalf@y
                        \let\pgfplots@cube@halfy=\pgfmathresult
                        \pgfplots@cube@gethalf@z
                        \let\pgfplots@cube@halfz=\pgfmathresult
                        \pgfmathparse{0*\pgfplots@cube@halfz}%
                        \let\pgfplots@cube@topz=\pgfmathresult
                        \pgfmathparse{-1*\pgfplots@cube@halfz}%
                        \let\pgfplots@cube@bottomz=\pgfmathresult
                        %
                        \pgfplotsifaxissurfaceisforeground{0vv}{%
                                \pgfsetfillcolor{\CubeFrontColor}
                                \pgfpathmoveto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{\pgfplots@cube@bottomz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{\pgfplots@cube@bottomz}}%
                                \pgfpathclose
                                \pgfusepathqfillstroke
                        }{% 
                                \pgfsetfillcolor{\CubeFrontColor}
                                \pgfpathmoveto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{\pgfplots@cube@bottomz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{\pgfplots@cube@bottomz}}%
                                \pgfpathclose
                                \pgfusepathqfillstroke
                        }%
                        \pgfplotsifaxissurfaceisforeground{v0v}{%
                                \pgfsetfillcolor{\CubeSideColor}
                                \pgfpathmoveto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{\pgfplots@cube@bottomz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{\pgfplots@cube@bottomz}}%
                                \pgfpathclose
                                \pgfusepathqfillstroke
                        }{% 
                                \pgfsetfillcolor{\CubeSideColor}
                                \pgfpathmoveto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{\pgfplots@cube@bottomz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{\pgfplots@cube@bottomz}}%
                                \pgfpathclose
                                \pgfusepathqfillstroke
                        }%
                        \pgfplotsifaxissurfaceisforeground{vv0}{%
                                \pgfsetfillcolor{\CubeTopColor}
                                \pgfpathmoveto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{\pgfplots@cube@bottomz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{\pgfplots@cube@bottomz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{\pgfplots@cube@bottomz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{\pgfplots@cube@bottomz}}%
                                \pgfpathclose
                                \pgfusepathqfillstroke
                        }{% 
                                \pgfsetfillcolor{\CubeTopColor}
                                \pgfpathmoveto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathclose
                                \pgfusepathqfillstroke
                        }%
            }
\makeatother
\begin{document}
\begin{tikzpicture}[scale=1, transform shape]
\pgfmathsetmacro{\gconv}{63.85498}%<- you get told what you need to put here
\begin{axis}[
view={110}{20},
width=10cm,
height=10cm,
grid=major,
xmin=0,xmax=4.5,
ymin=0,ymax=5.5,
zmin=0,%zmax=1.5,
xtick={1,2,3,4},
xticklabels={L1, L2, L3, L4},
ytick={1,2,3,4,5},
yticklabels={A, B, C, D, E},
ylabel={$y$},
xlabel={$x$},
zlabel={$z$},
axis equal,
]
\path let \p1=($(axis cs:0,0,1)-(axis cs:0,0,0)$) in 
\pgfextra{\pgfmathsetmacro{\conv}{2*\y1}
\ifx\gconv\conv
\typeout{z-scale\space good!}
\else
\typeout{Kindly\space consider\space setting\space the\space 
        prefactor\space of\space z\space to\space \conv}
\fi     
        };  

\pgfplotsset{3d bars/.style={only marks,scatter,mark=half cube*,mark size=0.6cm, 
3d cube color=#1,point meta=0,
,visualization depends on={\gconv*z \as \myz},
scatter/@pre marker code/.append style={/pgfplots/cube/size z=\myz},}}

\addplot3[3d bars=blue]
    coordinates {(1,1,0.7)(1,2,0.5)(1,3,0.9)(1,4,0.4)(1,5,0.33)};

\addplot3[3d bars=orange]
    coordinates {(2,1,0.6)(2,2,0.3)(2,3,0.65)(2,4,0.67)(2,5,0.31)};

\addplot3[3d bars=violet]
    coordinates {(3,1,0.3)(3,2,0.45)(3,3,0.7)(3,4,0.89)(3,5,0.45)};

\addplot3[3d bars=red]
     coordinates {(4,1,1)(4,2,0.2)(4,3,0.6)(4,4,0.72)(4,5,0.78)};

\end{axis}
\end{tikzpicture}
\end{document} 

在此处输入图片描述

如果您想知道为什么条形图如此小:这是因为键axis equal。您可能不想知道为什么我没有通过将转换因子写入辅助文件并从​​那里恢复来使其更优雅,对吗?

相关内容