5
$\begingroup$

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

enter image description here

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

enter image description here

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?

$\endgroup$
  • 2
    $\begingroup$ Search the docs for "GradientScaleCube"... perhaps $\endgroup$ – Dr. belisarius Jun 2 '15 at 3:39
  • $\begingroup$ 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. $\endgroup$ – bbgodfrey Jun 2 '15 at 4:17
4
$\begingroup$

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, 
    ChartElementFunction -> ChartElementDataFunction["GradientScaleCube", 
      "ColorScheme" -> #]] & /@ {"Rainbow", "SolarColors"}]

enter image description here

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], 
   {3, 0, 0} -> ChartElementDataFunction["GradientScaleCube", 
     "ColorScheme" -> "Rainbow"],
   {1, 0, 2} -> ChartElementDataFunction["SegmentScaleCube", 
     "ColorScheme" -> "DarkRainbow"]};
data3 = {{0, 0, 1 -> ChartElementDataFunction["GradientScaleCube", 
      "ColorScheme" -> "Rainbow"]},
     {3 -> ChartElementDataFunction["GradientScaleCube", 
      "ColorScheme" -> "Rainbow"], 0, 0}, 
     {1, 0, 2 -> ChartElementDataFunction["SegmentScaleCube", 
      "ColorScheme" -> "DarkRainbow"]}};

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

enter image description here

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

dt = Reverse[Range[3]] # & /@ Range[3];
grads = RandomChoice[ColorData["Gradients"][[11 ;; 16]], 9];

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, 
      ChartElementDataFunction["GradientScaleCube", "ColorScheme" -> #] & /@ grads}], 3];

and define the custom ChartElementFunction as:

cedF2 = #3[[1]][##] &;

Example:

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

enter image description here

$\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.