# Default Chart Color Doesn't Match

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


BarChart[Range[5], ChartStyle -> 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]]


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

 BarChart[{Range[5]}]


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]


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

-

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

ChartingCommonDumprogerStyles[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}]


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

-
+1! Care to share how you sleuthed this one out? –  bobthechemist Feb 12 at 21:50
@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. –  Simon Woods Feb 12 at 22:10
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. –  Mr.Wizard Feb 13 at 1:22
@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) –  Simon Woods Feb 13 at 10:00
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?? –  István Zachar Feb 18 at 0:48

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


-
AFAIK all default filling has an opacity term. –  Mike Honeychurch Feb 12 at 20:47

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]


-
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. –  user2946082 Feb 12 at 19:24

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.

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[#] &]


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


-
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]]] –  user2946082 Feb 19 at 15:17

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

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


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


colours == ChartingCommonDumprogerStyles[5]
(* True *)
`
-