Can the frame border on a BarChart Legend be removed?

吃可爱长大的小学妹 提交于 2019-11-30 20:14:32

You can temporarily, globally kill the frame by setting:

SetOptions[Legending`GridLegend, Legending`LegendContainer -> Identity]

To restore the default behavior, set:

SetOptions[Legending`GridLegend, Legending`LegendContainer -> Automatic]
Simon

I can't find any options to turn the frame off. The documentation for LegendAppearance is fairly minimal and the styling of legends in general does not get much discussion (see [2] and links within).

The easiest solution I can think of is to manually modify the graphics. Charts with legends produce Labeled graphics objects. For a single legend, the Labeled object that is produced looks like Labeled[Graphics[...], Framed[...], pos], so all you need to do is remove the Framed part. This could be done just be removing all Framed heads using a ReplaceAll (e.g. BarChart[...] /. Framed -> Identity), but maybe something more targeted would be safer.

mydata = {4.5644, 5.546, 6.8674, 2.7688, 1.742, 5.3952, 4.3392, 
   4.5016, 3.7748, 1.838, 2.24, 0.693, 2.818, 4.9, 3.939, 3.459, 
   3.755, 4.475, 3.857, 3.215, 2.206, 2.206, 2.117, 3.403, 3.277, 
   3.761, 4.276, 2.559, 3.486, 4.778, 2.281, 2.865, 3.629, 4.916, 
   4.572, 5.244, 5.395, 2.865, -0.524, 5.01, 4.401, 4.513, 4.54};

bc = BarChart[{Legended[Style[mydata[[;; -4]], Red], "Data"], 
   Legended[Style[mydata[[-3 ;;]], Blue], "Forecasts"]}, 
  PlotRange -> {-2, 8}, BarSpacing -> 0.4, LegendAppearance -> "Row"]

bc /. Labeled[g_, Framed[leg_], pos_] :> Labeled[g, leg, pos]

The above could also be produced using Replace[bc, Framed[leg_] :> leg, {1}] or MapAt[Apply[Identity, #] &, bc, 2] or similar constructions. It wouldn't take much to modify the code if you have more labels or different types of graphics objects.

tomd

Not as versatile as Simon's method given above, but nevertheless possibly worth posting. (I found this out while reading this question)

Using Part, where bc is as defined in Simon's answer:

bc[[2]] = bc[[2, 1]]; bc

giving

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!