3
$\begingroup$

I came across this and found it quite cool.

Here is the minimal example:

 Plot3D[
   Re[Sin[x + I y]], 
   {x, -2 Pi, Pi},
   {y, -2 Pi, 2 Pi}, 
   ColorFunction -> "AvocadoColors", 
   PlotLegends -> {
     Placed[BarLegend[Automatic, LabelStyle -> {14, GrayLevel[0.3]}], Right], 
     Placed[BarLegend[Automatic, LabelStyle -> {14, GrayLevel[0.3]}], Top]
   }
]

I want to do the same for a BarChart and do the equivalent of:

BarChart[
  {1,2,10,20},
  ChartStyle -> {Red,Blue, Darker[Red], Darker[Blue]},
  ChartLegends-> {
    Placed[{"One", "Two"}, Top],
    Placed[{"One", "Two", "Ten", "Twenty"}, Left]
  }
]

However the above simply gives me one legend with the last set of values. what am I missing? is there an elegant way of getting it to work?

$\endgroup$

1 Answer 1

1
$\begingroup$

One possible way is to use two datasets as input:

BarChart[{{1, 2, 10, 20}, {}}, 
  ChartStyle -> {Red, Blue, Darker[Red], Darker[Blue]}, 
  ChartLegends -> 
    {Placed[SwatchLegend[{Red, Blue}, {"One", "Two"}], Top],
     Placed[{"One", "Two", "Ten", "Twenty"}, Left]}] 

enter image description here

Another way is to wrap input data with Legended

BarChart[MapThread[Legended[#, Placed[#2, Left]] &, 
     {{1, 2, 10, 20}, {"One", "Two", "Ten", "Twenty"}}], 
  ChartStyle -> {Red, Blue, Darker[Red], Darker[Blue]}, 
  ChartLegends -> Placed[{"One", "Two"}, Top]] 

enter image description here

Use ChartStyle -> "Rainbow" to get

enter image description here

The first method requires manual insertion of appropriate colors in the first SwatchLegend to get the same picture, i.e., to use

 ChartLegends -> {Placed[SwatchLegend[ColorData[{"Rainbow",{1,4}}]/@{1,2}, 
    {"One", "Two"}], Top], 
  Placed[{"One", "Two", "Ten", "Twenty"}, Left]}
$\endgroup$

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.