12
$\begingroup$

I have a list like

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

Mathematica graphics

We can plot it with MatrixPlot directly with 2D style

MatrixPlot[list]

Mathematica graphics

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

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

Mathematica graphics

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?

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

You can use ListPlot3D with InterpolationOrder -> 0:

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

Mathematica graphics

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.

$\endgroup$
  • $\begingroup$ Fun,if you can make the color of those transparent pillar same to its top-face,I will accept this answer for its concise. $\endgroup$ – yode May 4 '17 at 16:53
  • $\begingroup$ @yode Yeah, now that is the hard part I hoped you won't ask for : ) $\endgroup$ – István Zachar May 4 '17 at 18:01
  • 1
    $\begingroup$ Thanks for the accept, but I should admit that coloring boxes according to top color seems impossible for ListPlot3D. $\endgroup$ – István Zachar May 4 '17 at 18:17
  • $\begingroup$ Yep,I have tried that just. But I like this solution still. :) $\endgroup$ – yode May 4 '17 at 18:30
13
$\begingroup$

Graphics primitives is quite elegant:

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

Mathematica graphics

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}]

Mathematica graphics

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

Mathematica graphics

$\endgroup$
  • 2
    $\begingroup$ If MatrixPlot[]'s coloring is desired, here is the necessary color function. $\endgroup$ – J. M. will be back soon Apr 25 '17 at 6:23
  • $\begingroup$ @J.M. Nice, especially because of the way it was found out. Thank you for sharing. $\endgroup$ – C. E. Apr 25 '17 at 15:26
4
$\begingroup$

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]

enter image description here

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.

$\endgroup$
4
$\begingroup$

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}] &

Mathematica graphics

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"]

Mathematica graphics

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}]])}}

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.