3
$\begingroup$

Create a simple function with arguments {i,j}, and make a Table for an array of values. For example:

f[i_, j_] := i*j; 
data = Table[f[i, j], {i, 1, 25}, {j, 1, 25}]

I want to create a 3D 'bar chart' of data - i.e., a plot that has a column of height f[i, j] at each Cartesian point {i,j} (ideally a block-shaped column with width and depth 1 centred on {i,j}. It sounds dead simple...

But ListPlot3D creates a meshed surface, not a series of columns, and I can't see an option to change this. And Histogram3D doesn't accept the format of data, and in any case I can't get my head around how it generates 3D information from pairs containing only 2 data points each - it's set up for data sampling, not plotting.

Suggestions?

$\endgroup$
1
  • $\begingroup$ What about ListPlot3D[data, InterpolationOrder -> 0, MeshStyle -> None, ColorFunction -> Hue]? $\endgroup$
    – chris
    Jun 27, 2020 at 11:37

2 Answers 2

3
$\begingroup$

You could try using some Cuboid's:

f[i_, j_] := i*j;
data = Table[f[i, j], {i, 1, 25}, {j, 1, 25}];

bar[{i_, j_}, h_] := 
 Cuboid[{i - 0.5, j - 0.5, 0}, {i + 0.5, j + 0.5, h}]
Graphics3D[{
  MapIndexed[bar[#2, #1] &, data, {2}]
  }, BoxRatios -> 1
 ]

cuboid chart

... and here I've spiced it up with colour, axes, and cylinders:

huescale = MinMax[data];
bar[{i_, j_}, h_] := {Hue[Rescale[h, huescale]], 
  Cylinder[{{i, j, 0}, {i, j, h}}, 0.5]}
Graphics3D[{
  MapIndexed[bar[#2, #1] &, data, {2}]
  }, BoxRatios -> 1, Axes -> True, AxesLabel -> {i, j, f}, 
 Lighting -> "Neutral"
]

cylinders chart

$\endgroup$
1
$\begingroup$

You may use Histogram3D and WeightedData,

wdata =
 WeightedData[
  Flatten[Table[{i, j}, {i, 25}, {j, 25}], 1],
  Apply[f]
  ]

Mathematica graphics

Then

Histogram3D[wdata, {{1}, {1}}]

enter image description here

See options like ChartElementFunction, ColorFunction, and other for appearance customization.

Hope this helps.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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