1
$\begingroup$

I want to put a custom bar legend beside the plots.

barleg= BarLegend[{Blend[{{0, Hue[0., 0.1, 1]}, {1, Hue[0., 0.8, 1]}, {1.01, 
      Hue[0.3, 0.1, 1]}, {2, Hue[0.3, 0.8, 1]}, {2.01, 
      Hue[0.7, 0.1, 1]}, {3, Hue[0.7, 0.8, 1]}}, #] &, {0, 3}}, 
 "Ticks" -> {0, 1, 2, 3}, "TickLabels" -> {"0.1", "1", "10", "100"}, 
 LegendLabel -> 
  Style[HoldForm["\[Epsilon]"], 20, Black, FontFamily -> "Times"], 
 LabelStyle -> { FontFamily -> "Times", FontSize -> 20}, 
 LegendLayout -> "Row"]

gives what I want to attach:

enter image description here

But when I put this same command in Plot function, it changes simply to red

Plot[{}, {x, 0, 1}, 
 PlotLegends -> 
  Placed[BarLegend[{Blend[{{0, Hue[0., 0.1, 1]}, {1, 
         Hue[0., 0.8, 1]}, {1.01, Hue[0.3, 0.1, 1]}, {2, 
         Hue[0.3, 0.8, 1]}, {2.01, Hue[0.7, 0.1, 1]}, {3, 
         Hue[0.7, 0.8, 1]}}, #] &, {0, 3}}, "Ticks" -> {0, 1, 2, 3}, 
    "TickLabels" -> {"0.1", "1", "10", "100"}, 
    LegendLabel -> 
     Style[HoldForm["\[Epsilon]"], 20, Black, FontFamily -> "Times"], 
    LabelStyle -> { FontFamily -> "Times", FontSize -> 20}], Right]
 ]

gives

enter image description here

How do I get the exact bar legend with the plot?

$\endgroup$

1 Answer 1

1
$\begingroup$

Add the option ColorFunctionScaling -> False to BarLegend:

Plot[{}, {x, 0, 1}, 
 PlotLegends -> 
  Placed[BarLegend[{Blend[{{0, Hue[0., 0.1, 1]}, {1, 
         Hue[0., 0.8, 1]}, {1.01, Hue[0.3, 0.1, 1]}, {2, 
         Hue[0.3, 0.8, 1]}, {2.01, Hue[0.7, 0.1, 1]}, {3, 
         Hue[0.7, 0.8, 1]}}, #] &, {0, 3}}, "Ticks" -> {0, 1, 2, 3}, 
    "TickLabels" -> {"0.1", "1", "10", "100"}, 
    LegendLabel -> Style[HoldForm["ϵ"], 20, Black, FontFamily -> "Times"], 
    LabelStyle -> {FontFamily -> "Times", FontSize -> 20}, 
    ColorFunctionScaling -> False], Right]]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.