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:

   Inset[barLegend, {1, 1, 1}]}]]

enter image description here

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}



2 Answers 2


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



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.

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


  • $\begingroup$ 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?! $\endgroup$ Mar 23, 2017 at 0:31
  • $\begingroup$ @DavidG.Stork. That is because the frame is drawn after the legend. $\endgroup$
    – m_goldberg
    Mar 23, 2017 at 0:33
  • $\begingroup$ So is there any way to "occlude" the frame by the legend? $\endgroup$ Mar 23, 2017 at 0:42
  • $\begingroup$ m_goldberg: Hooray... Thanks very much. That works. $\endgroup$ 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"}

enter image description here

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

enter image description here

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

enter image description here

  • $\begingroup$ 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. $\endgroup$ Mar 23, 2017 at 0:29
  • $\begingroup$ @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. $\endgroup$ Mar 23, 2017 at 0:32
  • $\begingroup$ 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. $\endgroup$ Mar 23, 2017 at 0:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.