# BarChart3D: varying individual bar color

I've been working with a chart like so:

data = {{0, 0, 1}, {3, 0, 0}, {1, 0, 2}} + .001;
BarChart3D[data, ChartLayout -> "Grid", Method -> {"Canvas" -> None},
Axes -> None, FaceGrids -> None, BarSpacing -> 0,
ViewPoint -> 1.5 {3, -2.29, 3},
ColorFunction -> "Rainbow"]


Right now the entirety of each bar is rendered in some color based on its height. What I'd like is for the color of each bar to vary in the vertical direction, an effect similar to, for example

Plot3D[Sin[x + y^2], {x, -3, 3}, {y, -2, 2},
ColorFunction -> (Blend[{Yellow, Purple}, #3] &)]


where the color of any point on the surface depends only on its z position. ColorFunction seems to offer a lot of control over the colors of objects; is there a way to specify a ColorFunction for each bar, or even all bars uniformly?

• Search the docs for "GradientScaleCube"... perhaps Commented Jun 2, 2015 at 3:39
• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory Tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. Commented Jun 2, 2015 at 4:17

You can use ChartElementDataFunction["GradientScaleCube", "ColorScheme" -> cs] as the option value for ChartElementFunction to apply a gradient scheme cs to all data.

data = {{0, 0, 1}, {3, 0, 0}, {1, 0, 2}};
options = {ChartLayout -> "Grid", Method -> {"Canvas" -> None},
Axes -> None, FaceGrids -> None, BarSpacing -> 0,
ViewPoint -> 1.5 {3, -2.29, 3}, ImageSize -> 400};

Row[BarChart3D[data, options,
"ColorScheme" -> #]] & /@ {"Rainbow", "SolarColors"}]


is there a way to specify a ColorFunction for each bar

You can wrap each data set or each data point with the ChartElementFunction you wish to use. With "GradientScaleCube" as the ChartElementFunction you can inject your desired ColorFunction for a bar as the "ColorScheme" option:

data2 = {{0, 0, 1} -> ChartElementDataFunction["SquareWaveCube",
"AngularFrequency" -> 10, "RadialAmplitude" -> 0.26],
"ColorScheme" -> "Rainbow"],
{1, 0, 2} -> ChartElementDataFunction["SegmentScaleCube",
"ColorScheme" -> "DarkRainbow"]};
data3 = {{0, 0, 1 -> ChartElementDataFunction["GradientScaleCube",
"ColorScheme" -> "Rainbow"]},
"ColorScheme" -> "Rainbow"], 0, 0},
{1, 0, 2 -> ChartElementDataFunction["SegmentScaleCube",
"ColorScheme" -> "DarkRainbow"]}};

Row[BarChart3D[#, options,
ChartElementFunction -> (If[#3 =!= {}, #3[[1]][#],
ChartElementDataFunction["DoubleProfileCube"][##]] &)] & /@ {data2, data3}]


Update: Using custom ChartElementFunctions to inject bar-specific color functions through metadata wrappers:

dt = Reverse[Range[3]] # & /@ Range[3];


To use color gradients as metadata, use pointi -> colorgradiendti to assign colorgradiendti to pointi:

dt1 = Partition[Rule @@@ Transpose[{Flatten@dt, grads}], 3];


and define the custom ChartElementFunction as:

cedF1 = ChartElementDataFunction["GradientScaleCube",
"ColorScheme" -> If[#3 =!= {}, #3[[1]], "Pastel"]][##] &;


To use ChartElementFunction as metadata, use pointi -> ChartElementDataFunction["GradientScaleCube", "ColorScheme" -> colorgradiendti] to assign colorgradiendti to pointi:

dt2 = Partition[Rule @@@ Transpose[{Flatten@dt,

and define the custom ChartElementFunction as:
cedF2 = #3[[1]][##] &;

Row[{BarChart3D[dt1, options, ChartElementFunction -> cedF1],