4
$\begingroup$

I have this BarLegend and want to add Ticks at the middle of each small color bar in the BarLegend

BarLegend[{Blend[{Red, Yellow, Brown, Blue, Green, Black}, #] &, {0, 
   1}}, 5, Ticks -> None]    

enter image description here

I want to add ticks similar to this

enter image description here

here is my try but did not work.

BarLegend[{Blend[{Red, Yellow, Brown, Blue, Green, Black}, #] &, {0,1}}, 5,Ticks -> {{0, "0.0"}, {0.2, "0.2"}, {0.4, "0.4"}, {0.6,"0.6"}, {0.8, "0.8"}, {1,"1.0"}}]    

enter image description here

$\endgroup$
2
  • $\begingroup$ I do not think that you can do this. By definition, the lowest edge corresponds to the lowest value und correspondingly for the upper edge. $\endgroup$ Commented Aug 21, 2022 at 16:19
  • $\begingroup$ but there should be some workarounds! $\endgroup$
    – MMA13
    Commented Aug 21, 2022 at 17:44

2 Answers 2

4
$\begingroup$

As a workaround, you might consider

SwatchLegend[{Red, Yellow, Brown, Blue, Darker@Green, Black},
  Style[#, Bold] & /@ {"0.0", 0.2, 0.4, 0.6, 0.8, "1.0"},
  LegendLayout -> "ReversedColumn",
  LegendMarkerSize -> 40
 ]

enter image description here

$\endgroup$
1
  • $\begingroup$ what if I have 4 colors like BarLegend[{Blend[{Red, Yellow, Black, Blue}, #] &, {0, 1}}, 5, Ticks -> {{0, "0.0"}, {0.2, "0.2"}, {0.4, "0.4"}, {0.6, "0.6"}, {0.8, "0.8"}, {1, "1.0"}}] OR BarLegend[{"Rainbow", {0, 1}}, 5, Ticks -> {{0, "0.0"}, {0.2, "0.2"}, {0.4, "0.4"}, {0.6, "0.6"}, {0.8, "0.8"}, {1, "1.0"}}] $\endgroup$
    – MMA13
    Commented Aug 22, 2022 at 3:45
3
$\begingroup$

Here I managed to do it like

BarLegend[{Blend[{{0, Red}, {0.2, Red}, {0.201, Yellow}, {0.4, 
      Yellow}, {0.401, Brown}, {0.6, Brown}, {0.601, Blue}, {0.8, 
      Blue}, {0.801, Darker@Green}, {1, Darker@Green}, {1, 
      Black}, {0.2, Black}}, #] &, {0, 1.2}}, 
 Ticks -> Table[{i + 0.1, ToString[NumberForm[i, {2, 1}]]}, {i, 0, 1, 
    0.2}], ColorFunctionScaling -> False, 
 LabelStyle -> {FontFamily -> "Times", Black, FontSize -> 12}]     

enter image description here

I would appreciate it if you have an elegant way to do it automatically.

Now, my problem is that if we don't specify each color, e.g.

BarLegend[{"Rainbow", {0, 1}}, 5,   Ticks -> {{0, "0.0"}, {0.2, "0.2"}, {0.4, "0.4"}, {0.6, "0.6"}, {0.8,      "0.8"}, {1, "1.0"}}]       

enter image description here

How can I do it now?

$\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.