Can the frame border on a BarChart Legend be removed?

前端 未结 3 1918
别那么骄傲
别那么骄傲 2021-01-05 11:48

I am creating an application for various kinds of plot/chart drawing in Mathematica. Ultimately it will have a GUI, but the first step is to get the code right, and simple e

相关标签:
3条回答
  • 2021-01-05 11:55

    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]
    
    0 讨论(0)
  • 2021-01-05 12:03

    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

    enter image description here

    0 讨论(0)
  • 2021-01-05 12:14

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

    with frame

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

    with frame removed

    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.

    0 讨论(0)
提交回复
热议问题