8
$\begingroup$

According to the documentation

BarLegend is displayed in StandardForm as a graphics object

and it do looks like a normal graphics in front end.

However,

BarLegend["Rainbow"]//FullForm

still gives BarLegend["Rainbow"]

and

Show[BarLegend["Rainbow"]]

will give errors

Show::gtype: BarLegend is not a type of graphics.

So is there a way to convert legends to graphics objects?

$\endgroup$

2 Answers 2

8
$\begingroup$
First @ Cases[
  ToBoxes @ BarLegend["Rainbow"],
  gr_GraphicsBox :> ToExpression[gr],
  ∞
]
$\endgroup$
1
  • $\begingroup$ Oh, my god. Any explanations why you are so fast? : ) $\endgroup$
    – matheorem
    May 8, 2018 at 13:59
2
$\begingroup$

Also

ToExpression[MakeBoxes[BarLegend["Rainbow"]][[1,1]]][[1]]
$\endgroup$
4
  • $\begingroup$ It works! Thank you so much! $\endgroup$
    – matheorem
    May 9, 2018 at 0:24
  • $\begingroup$ @kglr: Does this also work for SwatchLegend? I tried it with your code but did not worked... $\endgroup$
    – M.A.
    Mar 13, 2021 at 10:21
  • $\begingroup$ @M.A., for SwatchLegend you can use Cases[MakeBoxes[SwatchLegend[{Red, Green}, {"a", "b"}]], TooltipBox[a_, ___] :> Graphics @@ a, All]. (Similarly, for PointLegend and LineLegend) $\endgroup$
    – kglr
    Mar 13, 2021 at 23:59
  • $\begingroup$ @kglr what is TooltipBox? A function? $\endgroup$
    – M.A.
    Mar 15, 2021 at 8:17

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.