14
$\begingroup$

I have looked through other posts to find out what the default plot color is, but when I use it, the color doesn't seem to match. Can anyone explain why I'm seeing different output?

The reason I ask is that I'd like to provide higher level plotting function that have an optional color parameter. If you don't provide your own color, I'd like to use Mathematica's defaults...but it doesn't create the same output.

BarChart[Range[5]]

Default Mathematica BarChart

BarChart[Range[5], ChartStyle -> ColorData[1, 1]]

Mathematica BarChart with ColorData[1,1]

Thanks, Bill

Update: Thanks for the help so far. It looks like BarCharts get a different color set than regular plots, because ColorData[1, 1] is the default color for Plot and ListPlot:

Plot[Sin[x], {x, -Pi, Pi}, PlotStyle -> Thickness[0.02]]

Sin Plot

But I do like the color set that is the default for BarChart:

 BarChart[{Range[5]}]

BarChart with progressing colors

As I look through the available color pallets, I'm not sure I see those as an option:

Grid[Partition[
    Framed[Show[ColorData[#, "Image"], ImageSize -> 100], 
    FrameStyle -> Gray] & /@ ColorData["Indexed"], 6, 6, 1, {}], 
    Spacings -> .25]

color pallets

Some further reflections. It hit me that the colors used for the BarChart are actually some type of gradient. It seems to be a "pastel rainbow" if I can call it that. It goes from blue to red, but it's not the bright rainbow colors, and it seems to lock in the first and last colors while changing the colors of the bars in a smooth fashion according to some function (I will be trying to figure this out). You can see what I'm talking about if you plot the following: BarChart[{Range[40]}, ImageSize -> Large]

I tried matching this to one of the named gradients, but none of them match. That first bluish/purple color is not represented in any of these plots (was looking for Red and Green around 0.8 with a Blue around 1):

Plot[{ColorData[#, x][[1]], ColorData[#, x][[2]], 
    ColorData[#, x][[3]]}, {x, 0, 1}, PlotStyle -> {Red, Green, Blue},
    PlotLabel -> #] & /@ ColorData["Gradients"]
$\endgroup$
0

6 Answers 6

16
$\begingroup$

The default colours for charts are defined by Charting`CommonDump`rogerStyles. For up to five different colours the values are hard-coded:

Charting`CommonDump`rogerStyles[5]

(* {RGBColor[0.798413, 0.82472, 0.968322], 
  RGBColor[0.733333, 1., 0.833722], RGBColor[1, 0.986999, 0.742123], 
  RGBColor[1, 0.860624, 0.662562], RGBColor[1, 0.696086, 0.721935]} *)

For greater than 5 colours a different scheme is used:

GraphicsRow[BarChart[{Range[#]}] & /@ {5, 6}]

enter image description here

These are obtained using this formula for $n$ colours:

Lighter[#1, 0.66] & /@ Table[Hue[0.6 (1 - i)], {i, 0, 1, 1/(n - 1)}]
$\endgroup$
8
  • 1
    $\begingroup$ +1! Care to share how you sleuthed this one out? $\endgroup$ Feb 12, 2014 at 21:50
  • $\begingroup$ @bobthechemist, I found it a while ago, looking for the colours used by CommunityGraphPlot so that I could create a legend. I think I probably used Trace to track it down. $\endgroup$ Feb 12, 2014 at 22:10
  • $\begingroup$ I couldn't seem to find this with Trace. If you remember or recreate the discovery please let me know; I'm always happy to improve my skills. $\endgroup$
    – Mr.Wizard
    Feb 13, 2014 at 1:22
  • $\begingroup$ @Mr.Wizard, rogerStyles appears (in v9) in the trace for multi-colour bar charts, e.g. Trace[BarChart[{{1,2,3}}], TraceInternal -> True] but not for single-colour charts (it looks like the light blue is hard coded) $\endgroup$ Feb 13, 2014 at 10:00
  • 3
    $\begingroup$ Nice finding! Any idea where does the context "Charting`" come from? Is it a file package or built-in? Who injects this context into the kernel? And who the hell is roger?? $\endgroup$ Feb 18, 2014 at 0:48
7
$\begingroup$

I've seen this answer around here before, but it looks as if default colors include an opacity term:

BarChart[Range[5], 
 ChartStyle -> {Directive[{Opacity[0.21], ColorData[1, 1]}]}]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ AFAIK all default filling has an opacity term. $\endgroup$ Feb 12, 2014 at 20:47
6
$\begingroup$

Simon Woods nailed it, thank you. I think it's interesting that exact colors are used when 5 or fewer bars are in the chart. It's great to know those values now. I took at look at the color composition of those, and they didn't quite match the formula I found by plotting the color contents.

enter image description here

I thought I'd provide details for the work I had done to mimic the color function the BarChart seems to be using. The function that Simon provided is the exact answer, but you have to provide the n value to get your color function. This function will give you the RGBColor for the [0-1] interval.

barChartColor[x_] := RGBColor[
    Max[0.66,Min[0.11357142857142866 + 1.2142857142857142*x, 1.0]], 
    If[x < .5,
        Max[0.66, Min[0.796 + 1.02*x, 1.0]], 
        Max[0.66, Min[1.9192592592592592 - 1.259259259259259*x, 1.0]]], 
    Max[0.66, Min[1.272 - 1.36*x, 1.0]]
]

This gives a spectrum like the following (cbar function complements of Szabolcs Horvát here):

cbar[cfun_] := 
  DensityPlot[x, {x, 0, 1}, {y, 0, 1}, 
    AspectRatio -> 1/7, Frame -> False, ColorFunction -> cfun]
(* Example Usage: *) 
   (* cbar["Rainbow"] *)
   (* cbar[Blend[{Red, Green, Blue}, #] &] *)
cbar[barChartColor[#] &]

enter image description here

BarChart[Range[60], ColorFunction -> barChartColor]

enter image description here

$\endgroup$
1
  • $\begingroup$ Here's a cleaner function: barChartColor[x_] := RGBColor[Max[2/3, Min[1/9 + 17/14*x, 1]], If[x < 1/2, Max[2/3, Min[4/5 + x, 1]], Max[2/3, Min[52/27 - 34/27*x, 1]]], Max[2/3, Min[5/4 - 7/5*x, 1]]] $\endgroup$ Feb 19, 2014 at 15:17
6
$\begingroup$

I usually use Cases, but it was interesting to read about rogerStyles.

chart = BarChart[{Range[5]}, ImageSize -> 100]

enter image description here

colours = Take[Cases[chart, RGBColor[__], Infinity],-5];
ColorSetter /@ colours

enter image description here

colours == Charting`CommonDump`rogerStyles[5]
(* True *)
$\endgroup$
5
$\begingroup$

I'm still digging through the Trace trying to figure out where it comes from, but the default color is:

default = RGBColor[0.798413061722744, 0.824719615472648, 0.968322270542458];

Test:

BarChart[Range[5], ChartStyle -> default]

enter image description here

$\endgroup$
1
  • $\begingroup$ That indeed does match--and I just realized you could get the color by pasting the Cell Expression in a text editor. But where it comes from is a mystery. I just looped through all the indexed colors and none of them match this default color. $\endgroup$ Feb 12, 2014 at 19:24
2
$\begingroup$

R. M. was asking about the default colors in the current version. I'm sure this is all common knowledge at this point, but I'm posting it here for convenience:

"ChartDefaultStyle" /. (Method /. Charting`ResolvePlotTheme["Default", BarChart])
   Blend[Lighter[System`PlotThemeDump`$ThemeDefaultGradient, 0.2], #1] &

System`PlotThemeDump`$ThemeDefaultGradient
   {RGBColor[0.97858, 0.678934, 0.157834], RGBColor[1., 0.43, 0.],
    RGBColor[0.73, 0.344, 0.], RGBColor[0.594326, 0.356, 0.653099],
    RGBColor[0.374, 0.444, 0.788663]}

As for the previous defaults:

"ChartDefaultStyle" /. (Method /. Charting`ResolvePlotTheme["Classic", BarChart])
   "Roger"

which is a bit uninformative; at least Simon shows how you get those colors.

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