# Same shading for the patches in the same plane (2)

I am trying to plot and shade a 3D figure using patch table with point meta as in question "Same shading for the patches in the same plane", but I am obtaining triangles in the same plane but with a different shade intensity. First I process my data with Mathematica to get the shades of a set of points.

In Mathematica:

R21 = {{0, 0, 0}, {0, 0, 0.436694}, {0, 0.436694, 0.436694}, {0,
0.5822, 0.2911}, {0, 0.873387, 0}, {0.291187, 0.5822,
0.2911}, {0.436694, 0.436694, 0.436694}, {0.873387, 0,
0}, {0.873387, 0, 0.436694}};

R = ConvexHullMesh[R21] And now I compute the list of vertices of each triangle of the figure with its color in the last column:

lookuptable =
Flatten[Nearest[R21 -> Automatic, MeshCoordinates[R]]]];

faces = Partition[
Lookup[lookuptable,
Flatten[MeshCells[R, 2, "Multicells" -> True][[1, 1]]]], 3];
closedfaces = Join[faces, faces[[All, {1}]], 2] - 1;

lsttriangles = {};
Do[x = R21[[closedfaces[[i, 1]] + 1]] - R21[[closedfaces[[i, 2]] + 1]];
y = R21[[closedfaces[[i, 1]] + 1]] -
R21[[closedfaces[[i, 3]] + 1]];
z = Cross[x, y];
AppendTo[lsttriangles,
Join[Take[closedfaces[[i]], 3], {Abs[z.{1, 2, 3}/Sqrt[z.z]]}]], {i,
1, Length[closedfaces]}];


where lsttriangles contains the list of vertices of the figure with the intensity-color data.

Using this data, I can plot the figure with this code:

\begin{tikzpicture}
\begin{axis}[view/v=25,view/h=126,xlabel=$a_1$,ylabel=$a_2$,zlabel=$a_3$,
% colormap/greenyellow,
colormap={darkblue}{color(1)=(blue!80!black!90) color(2)=(blue!80!black!70) color(3)=(blue!80!black!50) color(4)=(blue!80!black!30)},
xmin=0,xmax=1,ymin=0,ymax=1,zmin=0,zmax=1, grid=major]
opacity=0,
fill opacity=1,
table/row sep=\\,
patch,
patch type=polygon,
vertex count=3,
patch table with point meta={%
% pt1 pt2 pt3 pt4 pt5 cdata
0 4   7   3. \\
7 8   1   2. \\
8 6   2   3. \\
6 4   2   3.53553 \\
8 7   6   2.12132 \\
7 4   6   2.12132 \\
4 0   2   1. \\
0 7   1   2. \\
0 1   2   1. \\
1 8   2   3. \\
}
]
table {
x y z \\
0   0   0 \\
0   0   0.436694 \\
0   0.436694    0.436694 \\
0   0.5822  0.2911 \\
0   0.873387    0 \\
0.291187    0.5822  0.2911 \\
0.436694    0.436694    0.436694 \\
0.873387    0   0 \\
0.873387    0   0.436694 \\
};

\end{axis}
\end{tikzpicture}


However, The result shades the same plane with different colors: How can I make the triangles in the same plane to be equally colored? Points that uses Mathematica to plot the convex hull can be seen from InputForm[R]. I have tried the same process with these points, and the result has a similar effect in the figure.

I think the problem is to some extent on the Mathematica side. You are getting one excess triangle: 7 8 1 2.. If I remove it, I get \documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\usepgfplotslibrary{patchplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[view/v=25,view/h=126,xlabel=$a_1$,ylabel=$a_2$,zlabel=$a_3$,
% colormap/greenyellow,
colormap={darkblue}{color(1)=(blue!80!black!90) color(2)=(blue!80!black!70) color(3)=(blue!80!black!50) color(4)=(blue!80!black!30)},
xmin=0,xmax=1,ymin=0,ymax=1,zmin=0,zmax=1, grid=major]
opacity=0,
fill opacity=1,
table/row sep=\\,
patch,
patch type=polygon,
vertex count=3,
patch table with point meta={%
% pt1 pt2 pt3 pt4 pt5 cdata
0 4   7   3. \\
8 6   2   3. \\
6 4   2   3.53553 \\
8 7   6   2.12132 \\
7 4   6   2.12132 \\
4 0   2   1. \\
0 7   1   2. \\
0 1   2   1. \\
1 8   2   3. \\
}
]
table {
x y z \\
0   0   0 \\
0   0   0.436694 \\
0   0.436694    0.436694 \\
0   0.5822  0.2911 \\
0   0.873387    0 \\
0.291187    0.5822  0.2911 \\
0.436694    0.436694    0.436694 \\
0.873387    0   0 \\
0.873387    0   0.436694 \\
};
% removed
%              7 8   1   2. \\

%     opacity=1,
%     table/row sep=\\,nodes near coords/.append style={color=red},
%     only marks,nodes near coords=\coordindex
% ]
% table {
%     x y z\\
%             0   0   0 \\
%             0   0   0.436694 \\
%             0   0.436694    0.436694 \\
%             0   0.5822  0.2911 \\
%             0   0.873387    0 \\
%             0.291187    0.5822  0.2911 \\
%             0.436694    0.436694    0.436694 \\
%             0.873387    0   0 \\
%             0.873387    0   0.436694 \\
%         };
\end{axis}
\end{tikzpicture}
\end{document}


You could debug yourself by restoring the triangle, resurrecting the commented out \addplot and setting fill opacity=0.6,. This hints at the culprit. In principle, pgfplots should hide it, but I guess that since all its corners are on some visible edges, pgfplots decided to keep it.

• But, if the triangles are in the same plane why their colors are different? – user1993416 Jun 9 '18 at 15:52
• @user1993416 No, as you can see from the lower plot, the triangle 7--8--1 is not on the same plane. Rather, it is on the back, and should in principle be hidden by the other triangles. – user121799 Jun 9 '18 at 16:05
• even if I use opacity=1 it will appear. – user1993416 Jun 9 '18 at 16:06
• @user1993416 Yes. That's what I wrote in my answer. My feeling is that pgfplots get confused with that triangle, as I also wrote. – user121799 Jun 9 '18 at 16:29