Mathematica Asked on July 8, 2021
If I add a legend using
Legended[..., SwatchLegend[..., ...]]
then I can explicitly set the legend marker size using the LegendMarkerSize
option. But I also need to explicitly set the style/colour of the legend elements a second time within the legend expression.
It is usually much more convenient to use the PlotLegens
or the ChartLegends
option. It spares me the trouble of having to manually match up the legens with the plot or chart. If I use this, can I still control the marker size?
Example:
cm = 72/2.54;
BarChart[{{1, 2, 3}, {1, 3, 2}}, ChartLegends -> {"a", "b", "c"}, ImageSize -> 4 cm]
How can I control the legend marker size in the above graphic? Is there anything simpler than an explicit legend specification as below?
colours = {Red, Blue, Black};
Legended[
BarChart[{{1, 2, 3}, {1, 3, 2}}, ImageSize -> 4 cm, ChartStyle -> colours],
SwatchLegend[colours, {"a", "b", "c"}, LegendMarkerSize -> 7]
]
I always do it like this:
cm = 72/2.54;
BarChart[{{1, 2, 3}, {1, 3, 2}},
ChartLegends -> SwatchLegend[{"a", "b", "c"}, LegendMarkerSize -> 20], ImageSize -> 4 cm]
top: code from your question, bottom: this code
The colors of the legend are inherited from the BarChart object:
BarChart[{{1, 2, 3}, {1, 3, 2}}, ChartStyle -> {Red, Green, Blue},
ChartLegends -> SwatchLegend[{"a", "b", "c"}, LegendMarkerSize -> 20], ImageSize -> 4 cm]
Correct answer by DaveStrider on July 8, 2021
For ChartLegends
, although the documentation does not mention any values other than "Row"
and "Column"
for the option LegendAppearance
, it turns out you can also use this option to set the LegendMarkerSize
BarChart[{{1, 2, 3}, {1, 3, 2}}, ChartStyle -> {Red, Green, Blue},
LegendAppearance -> {LegendMarkerSize -> 20},
ChartLegends -> {"a", "b", "c"}, ImageSize -> 4 cm]
In fact, you can use any of the options for SwatchLegend
as the setting for LegendAppearance
:
Row[BarChart[{{1, 2, 3}, {1, 3, 2}}, ChartStyle -> {Red, Green, Blue},
ChartLegends -> {"a", "b", "c"}, ImageSize -> 4 cm,
LegendAppearance -> #] & /@ {
{LegendLabel -> "legend"},
{LabelStyle -> Red},
{LegendLayout -> "ReversedColumn", LegendMarkerSize -> 20},
{LegendLayout -> "ReversedRow", LegendMarkerSize -> 20,
LegendMarkers -> "Bubble"}}]
For PlotLegends
, this approach does not work.
Answered by kglr on July 8, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP