# How to plot a matrix with 3D style

I have a list like

SeedRandom
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
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? – J. M.'s discontentment Apr 25 '17 at 4:53
• @J.M. Good link,and “For complex matrices the modulus (absolute value) of each element is displayed” as the description. – yode Apr 25 '17 at 5:09
• This discussion might be of interest (or useful.) – Anton Antonov Apr 25 '17 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? – Anton Antonov Apr 25 '17 at 13:54
• Isn't this a duplicate of Height-dependent filling color in 3D Data Plots? – István Zachar May 4 '17 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 May 4 '17 at 16:53
• @yode Yeah, now that is the hard part I hoped you won't ask for : ) – István Zachar May 4 '17 at 18:01
• Thanks for the accept, but I should admit that coloring boxes according to top color seems impossible for ListPlot3D. – István Zachar May 4 '17 at 18:17
• Yep,I have tried that just. But I like this solution still. :) – yode May 4 '17 at 18:30

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. – J. M.'s discontentment Apr 25 '17 at 6:23
• @J.M. Nice, especially because of the way it was found out. Thank you for sharing. – C. E. Apr 25 '17 at 15:26

Also:

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


BarChart3D:

BarChart3D[Reverse /@ list, ChartLayout -> "Grid",
BarSpacing -> {0, 0}, ColorFunction -> "Rainbow",
"Canvas" -> False, "FaceGrids" -> None][] //
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][]}, {j, 1, Dimensions[list][]},
ExtentSize -> Full, FillingStyle -> Opacity, 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 Apr 25 '17 at 8:20
• It's very feauty here,thanks very much.And thanks for your link. – yode Apr 25 '17 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.