TransWikia.com

BarChart3D: varying individual bar color

Mathematica Asked by nadlr on May 13, 2021

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?

One Answer

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

Correct answer by kglr on May 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP