I am trying to change the 2D scatter graph into 3D which can also take the value of the P column as z-axis. But the output I got is not smooth and I really don't understand how I can change it into 3D graph which shows the regions where cost DR, BR or SC is optimal for (P:Q:R) coordinates. Can any expert please help me draw a nice 3D graph of the given data? In the code, I am using a minimum dataset for illustration purpose although the actual file is so big with many data points. Here is the code:


\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

    % 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
    % define how the different cases are drawn
    % all should have same mark and draw opacity,
    % but change the color as you prefer
        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
% 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}

enter image description here

  • +1 Nice minimal working example! Mar 6, 2021 at 18:51
  • So, the problem is that the drawed dots are not connected between each other? Isn't that the purpose of a scatter plot?
    – Jes
    Mar 6, 2021 at 21:34
  • @Jes! I want something like a surface that shows cluster/ regions where BR and SC and DR are optimal as per (P:Q: R) coordinates. I don't know in this case scatter works? or do I need to check /surf?
    – TaniaMalik
    Mar 6, 2021 at 22:40
  • @TaniaMalik :I think in this case surf is what you are looking for, but you will need a filter to select the values of SC, DR and BR and then plot them separately with a different color. At least that's what I can think of, 3d plots are more or less unknown to me :(
    – Jes
    Mar 7, 2021 at 14:24
  • I can see something like this solution working, but I cannot guarantee it
    – Jes
    Mar 7, 2021 at 14:27


You must log in to answer this question.

Browse other questions tagged .