# Set background color in BarLegend

I have a complex Graphics3D and would like to include a BarLegend, such as:

mylegend =
BarLegend[{Hue[#] &, {0, 1}},
LegendFunction -> "Frame",
LegendLabel -> Text[Style["time (s)", 16]]]


I then Insert and place it into the full figure this way:

Show[
Graphics3D[{Sphere[],
Inset[barLegend, {1, 1, 1}]}]]


This is almost what I seek. I would like mylegend to have a solid background color to thus obscure the 3D graphics behind it, including the frame.

I could use Placed and Framed and Background and work hard to achieve this. It would be so much simpler if a framed legend had the option of a background color, but as far as I can see from the documentation, it doesn't. Obvious attempts such as to following do not work:

BackgroundColor -> Yellow

Background -> Yellow

Style -> {Black, Background -> Yellow}


etc.

Perhaps this is what you are looking for.

barlegend =
BarLegend[{Hue[#] &, {0, 1}},
LegendFunction -> (Panel[#, Background -> Yellow] &),
LegendLabel -> Text[Style["time (s)", 16]]]

Graphics3D[{Sphere[], Inset[barlegend, {1, 1, 1}]}]


### Update

The following will force the legend to be drawn after the graphics box, but only allows positioning in the 2D coordinate of the graphics viewport.

Graphics3D[Sphere[],
Epilog -> Inset[barlegend, Scaled[{.8, .6}]]]


• Very very curious. Indeed, the yellow background obscures the sphere (thanks), but look carefully: The "back" of the frame is visible through the legend! What is going on?! Mar 23, 2017 at 0:31
• @DavidG.Stork. That is because the frame is drawn after the legend. Mar 23, 2017 at 0:33
• So is there any way to "occlude" the frame by the legend? Mar 23, 2017 at 0:42
• m_goldberg: Hooray... Thanks very much. That works. Mar 23, 2017 at 1:08

In general you could do something like

{BarLegend["Rainbow", LabelStyle -> {Background -> LightBlue}],
BarLegend["Rainbow", LabelStyle -> {Background -> LightBlue}],
LegendFunction -> "Frame"}


But then you have little control of finer details, like that white gap between background and frame. So to help you with "hard work", I just adopt relevant example listed in docs page for the function BarLegend. That example in docs is basically telling you "this is how you currently suppose to do it".

frame[legend_] := Framed[legend, FrameStyle -> Red,
RoundingRadius -> 10, FrameMargins -> 10, Background -> LightGreen]

mylegend = BarLegend[{Hue[#] &, {0, 1}},
LegendFunction -> frame, LegendLabel -> Text[Style["time (s)", 16]]]


Show[Graphics3D[{Sphere[], Inset[mylegend, {1, 1, 1}]}]]


• Thanks @Vitaliy, but when I Insert your barlegend into a Graphics3D figure, for some reason it remains transparent and you can see the 3D figure (such as the frame) "through" it, as you can see from your figure. Mar 23, 2017 at 0:29
• @DavidG.Stork it works for me on mac in 11.1 as you can see from image i added to my reply. You are probably on WIN. This is probably related to an issue that is currently is being fixed and in future it will work. Mar 23, 2017 at 0:32
• I'm using v. 10.4 on a Mac. Regardless, on this website your bar legend does not obscure or occlude the frame as I hope to achieve. Mar 23, 2017 at 0:39