4
$\begingroup$

I would like to extract the colors (i.e., obtain RGBColor[] or other full specification) of the different colors used in a bar chart such as this

BarChart[{{4, 4, 1, 0.05}, {3, 3, 1, 1}, {1, 1, 2, 2}, {2, 2, 4, 4}}]

for use in other figures, text and such. I've tried Options[BarChart] and AbsoluteOptions[...] and found only that the options are Automatic. I have also looked at a number of other posted problems focusing on extracting specific numerical Option values when the value is Automatic and none yield the answer. Likewise controlling colors in BarChart is straightforward and not the problem I'm addressing.

$\endgroup$
7
  • 2
    $\begingroup$ Union@Cases[BarChart[....], _RGBColor, Infinity] is a first step ... $\endgroup$ Jan 11, 2016 at 16:58
  • $\begingroup$ The default colors are ColorData[97]. $\endgroup$
    – rcollyer
    Jan 15, 2016 at 14:43
  • $\begingroup$ @rcollyer Thanks so much. How did you find out that the default colors are ColorData[97]? $\endgroup$ Jan 15, 2016 at 16:42
  • 1
    $\begingroup$ @DavidG.Stork I work at wolfram. :) $\endgroup$
    – rcollyer
    Jan 15, 2016 at 16:58
  • $\begingroup$ @rcollyer OK, then... how do the rest of us find default values, such as colors? $\endgroup$ Jan 15, 2016 at 18:10

2 Answers 2

7
$\begingroup$
bar = 
 BarChart[{{4, 4, 1, 0.05}, {3, 3, 1, 1}, {1, 1, 2, 2}, {2, 2, 4, 4}}]

enter image description here

c = Cases[bar[[1]], _RGBColor, Infinity]

enter image description here

Union@c // InputForm

{RGBColor[0.4992, 0.5552, 0.8309304], RGBColor[0.7116405333333333, 0.4816, 0.5483194666666666], RGBColor[0.928, 0.5210666666666667, 0.2], RGBColor[0.982864, 0.7431472, 0.3262672]}

$\endgroup$
3
  • $\begingroup$ Thanks very much. I think Mathematica should allow access to colors and other Automatic options directly, but your solution works perfectly. $\endgroup$ Jan 11, 2016 at 17:09
  • $\begingroup$ Colors are not part of options but buried deep inside the graphic directives. I agree that there should be an easier way to identify the color scheme(s) used for a plot. Thanks for accept :) $\endgroup$
    – eldo
    Jan 11, 2016 at 17:16
  • $\begingroup$ A followup question: Is there a way to extract the color scheme under such circumstances? $\endgroup$ Jan 11, 2016 at 23:54
3
$\begingroup$

You can find the actual default style information using this:

Charting`ResolvePlotTheme["Default", BarChart]
{{0.71, "DefaultChartBaseStyle" -> RGBColor[0.982864, 0.7431472, 0.3262672]}, {0.1, 
  GridLinesStyle -> Directive[
GrayLevel[0.5, 0.4]]}, {0.71, 
  "ChartDefaultStyle" -> (Blend[
      Lighter[System`PlotThemeDump`$ThemeDefaultGradient, 0.2], #1] &)}, {0.1, 
  "AxisPadding" -> Scaled[0.02]}, {0.1, "DomainPadding" -> Scaled[0.02]}, {0.1, 
  "RangePadding" -> Scaled[0.05]}}

The key function:

stylefn = (Blend[Lighter[System`PlotThemeDump`$ThemeDefaultGradient, 0.2], #1] &);

And four styles as used in the chart:

Array[stylefn, 4, {0, 1}]
{RGBColor[0.982864, 0.7431472, 0.3262672],
 RGBColor[0.928, 0.5210666666666667, 0.2],
 RGBColor[0.7116405333333333, 0.4816, 0.5483194666666666],
 RGBColor[0.4992, 0.5552, 0.8309304]}

References:

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