# How to plot a matrix with 3D style

I have a list like

SeedRandom[1]
MatrixForm[list = RandomInteger[5, {5, 5}]]


We can plot it with MatrixPlot directly with 2D style

MatrixPlot[list]


I hope to plot it with 3D style.This is current method

Histogram3D[Catenate[Table @@@ Catenate[MapIndexed[{#2, #} &, list, {2}]]],
ColorFunction -> ColorData["Rainbow"]]


But I don't know how to plot a matrix with real number.Such as

SeedRandom[1]
MatrixForm[list = RandomReal[5, {5, 5}]]


Is there any elegant method can do this?

• Something like a cityplot, then. How do you want negative or complex entries to be handled? Commented Apr 25, 2017 at 4:53
• @J.M. Good link,and “For complex matrices the modulus (absolute value) of each element is displayed” as the description.
– yode
Commented Apr 25, 2017 at 5:09
• This discussion might be of interest (or useful.) Commented Apr 25, 2017 at 13:21
• @yode Why do you want to plot the matrix in 3D? Is it because the cell colors of MatrixPlot are not informative enough? Commented Apr 25, 2017 at 13:54
• Isn't this a duplicate of Height-dependent filling color in 3D Data Plots? Commented May 4, 2017 at 17:53

You can use ListPlot3D with InterpolationOrder -> 0:

ListPlot3D[list, InterpolationOrder -> 0, ColorFunction -> Hue,
Mesh -> None, Filling -> Axis]


But unfortunately I've failed to find a way to color the block under each square accordingly with FillingStyle or with other means domestic to ListPlot3D.

• Fun,if you can make the color of those transparent pillar same to its top-face,I will accept this answer for its concise.
– yode
Commented May 4, 2017 at 16:53
• @yode Yeah, now that is the hard part I hoped you won't ask for : ) Commented May 4, 2017 at 18:01
• Thanks for the accept, but I should admit that coloring boxes according to top color seems impossible for ListPlot3D. Commented May 4, 2017 at 18:17
• Yep,I have tried that just. But I like this solution still. :)
– yode
Commented May 4, 2017 at 18:30
• The original matrix was 5x5 and the plot is 4x4 Commented Nov 9, 2020 at 11:40

Graphics primitives is quite elegant:

box[h_, {x_, y_}] := Cuboid[{x, y, 0}, {x + 1, y + 1, h}]
Graphics3D@MapIndexed[box, list, {2}]


It is no less elegant with custom styling:

box[h_, {x_, y_}] := {
ColorData["Rainbow", h/Max[list]],
Cuboid[{x, y, 0}, {x + 1, y + 1, h}]
}
Graphics3D@MapIndexed[box, list, {2}]


Here's an example with the color function that is used by MatrixPlot, see J.M.'s comment below:

• If MatrixPlot[]'s coloring is desired, here is the necessary color function. Commented Apr 25, 2017 at 6:23
• @J.M. Nice, especially because of the way it was found out. Thank you for sharing. Commented Apr 25, 2017 at 15:26

Also:

SeedRandom[1]
list = RandomReal[5, {5, 5}];


BarChart3D:

BarChart3D[Reverse /@ list, ChartLayout -> "Grid",
BarSpacing -> {0, 0}, ColorFunction -> "Rainbow",
"Canvas" -> False, "FaceGrids" -> None][[1]] //
Graphics3D[#, Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}] &


DiscretePlot3D:

iF = Interpolation[Join @@ MapIndexed[Composition[Reverse, List], list, {2}]];

DiscretePlot3D[iF[i, j], {i, 1, Dimensions[list][[1]]}, {j, 1, Dimensions[list][[2]]},
ExtentSize -> Full, FillingStyle -> Opacity[1], ColorFunction -> "Rainbow"]


ListPlot3D:

Normal[ListPlot3D[list, InterpolationOrder -> 0, ColorFunction -> Hue, Mesh -> None]] /.
{Line[__] :> Sequence[], Polygon[x : {__},  VertexColors -> {col_, ___}, ___] :>
{col, EdgeForm[], Opacity[.9], Cuboid @@ ({{1, 1, 0}, 1} Sort[x][[{1, -1}]])}}


• – kglr
Commented Apr 25, 2017 at 8:20
– yode
Commented Apr 25, 2017 at 8:57

Here is a plot based on this answer of "Plotting “Terrain” with “Water” on them Using BarChart3D"

{n, m} = Dimensions[list];
{cx, cy} = {1/4, 1/4};
Graphics3D[{Red, Opacity[0.1],
Cuboid[{1/2, 1/2, 0}, {n + 1/2, m + 1/2, 0}],
Table[Tooltip[{Red, Opacity[0.1],
Cuboid[{i, j, 0}, {i + cx, j + cy, list[[i, j]]}], Blue,
Opacity[0.3],
Cuboid[{i, j, list[[i, j]]}, {i + cx, j + cy, list[[i, j]]}]},
BarChart[list[[i]], PlotLabel -> Row[{"row:", i}],
ChartLayout -> "Stacked"]], {i, n}, {j, m}]}, Axes -> True,
Boxed -> False, BoxRatios -> {n/Max[n, m], m/Max[n, m], 1/3},
ImageSize -> Large]


The idea is to make the plot more informative by (1) making the bars to be thinner and more transparent, and (2) providing a tooltip showing a 2D BarChart with stacked layout for each row.