在 Latex 中绘制 3D 图形

在 Latex 中绘制 3D 图形

我正在尝试将 2D 散点图更改为 3D,这样也可以将 P 列的值作为 z 轴。但我得到的输出并不平滑,我真的不明白如何将其更改为 3D 图形,以显示成本 DR、BR 或 SC 对于 (P:Q:R) 坐标最佳的区域。有哪位专家能帮我绘制给定数据的漂亮 3D 图形吗?在代码中,我使用最小数据集进行说明,尽管实际文件很大,包含许多数据点。代码如下:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}

\pgfplotstableread[col sep=comma]{
P, Q, R, cost
6, 3, 1, SC
6, 2, 2, SC
6, 1, 3, SC
5, 4, 1, DR
5, 3, 2, BR
5, 2, 3, DR
5, 1, 4, DR
4, 5, 1, DR
4, 4, 2, BR
4, 3, 3, BR
}\MyData

\begin{document}
    \begin{tikzpicture}
    % define a few macros for convenience
    \newcommand\AxisW{10cm} % width/height of axis
    \newcommand\AxisXMin{0.5} % xmin/ymin
    \newcommand\AxisRange{10} % range of axis, x y and z
    \begin{axis}[
    % define how the different cases are drawn
    % all should have same mark and draw opacity,
    % but change the color as you prefer
    scatter/classes={
        SC={mark=square*, draw opacity=0, fill=blue},
        DR={mark=square*, draw opacity=0, fill=red},
        BR={mark=square*, draw opacity=0, fill=green}
    },
% add labels for the axes
xlabel=R,
ylabel=Q,
zlabel=P,
% set up the size of the axis
scale only axis, % width/height applies to axis box alone, without labels
width=\AxisW, height=\AxisW,
% set up the range of the axes
xmin=\AxisXMin, xmax=\AxisXMin+\AxisRange,
ymin=\AxisXMin, ymax=\AxisXMin+\AxisRange,
zmin=\AxisXMin, zmax=\AxisXMin+\AxisRange,
% calculate the mark size - divide by 2 because the size is half the width
mark size={\AxisW/\AxisRange/2},
% set legend entries in one row
legend columns=-1,
% move legend outside top of axis
legend style={at={(0.5,1.02)}, anchor=south},
% have ticks every 1 unit distance
xtick distance=1,ytick distance=1,ztick distance=1,
] 

\addplot3 [
scatter, % make a scatter plot
only marks, 
scatter src=explicit symbolic 
]
table[x=R, % use R-column for x-values
y=Q, use Q-column for y-values
z=P, % use P-column for z-values
meta=cost % use cost-column to define which scatter class should be used
] \MyData;

\legend{SC, DR, BR}
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容