# How to change the color of specific ticks in BarLegend?

I would like to set the color of the ticks as in colorbar such that "Cold" is Cyan while "Hot" is Red

BarLegend[{{Red, Black, Cyan}, {0, 1}},
Ticks -> {{0, "Hot"}, {1, "Cold"}},
LabelStyle -> {FontSize -> 13, Black}, LegendMarkerSize -> 236]


• I posted a method that uses a defined ColorFunction if you find you want to use one of the interim colors in the future, rather than those at the endpoints. :D Not sure if it is entirely a better way to do it, but @kglr shows the key point of using Style to force the coloration of the labels. Apr 13, 2020 at 9:44

You can wrap the tick labels with Style:

BarLegend[{{Red, Black, Cyan}, {0, 1}},
Ticks -> {{0, Style["Hot", 13, Red]}, {1, Style["Cold", 13, Cyan]}},
LegendMarkerSize -> 236]


Perhaps more flexibly, (whoops... this part is just a variation of the approach already posted by CA Trevillian)

cf = Blend[{Red, Black, Cyan}, #] &;

BarLegend[{cf, {0, 1}},
Ticks -> MapThread[{#, Style[#2, 13, cf@#]} &, {{0, 1}, {"Hot", "Cold"}}],
LegendMarkerSize -> 236]


and

BarLegend[{cf, {0, 1}},
Ticks -> {0, 1},
"TickLabels" -> {Style["Hot", 13, cf@0], Style[ "Cold", 13,  cf@1]},
LegendMarkerSize -> 236]


and

BarLegend[{cf, {0, 1}},
"Ticks" -> {1, 0, .5},
"TickLabels" -> {"Hot", "Warm", "Cold"},
LabelingFunction -> (With[{col = cf[#]}, Style[#3, 13, col]] &)]


FWIW, using yet another undocumented option:

BarLegend[{cf, {0, 1}},
"RotateLabels" -> 45 Degree,
"Ticks" -> MapThread[{#, Style[#2, 13, cf@#]} &, {{0, 1}, {"Hot", "Cold"}}],
LegendMarkerSize -> 236]


• Beat me to it haha. Nice! I wonder if there's a way to make the marks for the Ticks change according to the ColorFunction supplied? I was gonna post what you did here as a sort-of non-automated workaround. Apr 13, 2020 at 9:26
• @CATrevillian, updated version shows a way to style the tick labels using the color function in the first argument of BarLegend.
– kglr
Apr 13, 2020 at 9:49
• That's awesome! (Your's is somehow cleaner than mine!) Clarification, must the "TickLabels" be defined in the order that they're supplied to Ticks? You could certainly beat me at codegolf with this! Apr 13, 2020 at 9:53
• LabelingFunction works as ”LabelingFunction” here too, at least on WolframCloud! This makes me wonder, just how many “undocumented” options are there that work for these?! Apr 13, 2020 at 14:43

If I understand correctly, this will produce what you want:

rbc := Blend[{Red, Black, Cyan}, #] &;

BarLegend[{rbc, {0, 1}},
Ticks ->
{
{#, Style["Hot", rbc[#]]}&@0,
{#, Style["Cold", rbc[#]]}&@1
},
LabelStyle -> {FontSize -> 13, Black}, LegendMarkerSize -> 236]


First, you recognize that BarLegend automatically creates a ColorFunction using Blend to combine the colors provided to it. Then, you can discover you may define your own! This is the rbc that I define above. Finally, with some usage of # and & to encourage anonymous function assignments, you may get the same image as @kglr, so I will not repeat that here.

I think this will provide you with the freedom to define such things as Ticks markings which change color according to the ColorFunction, but that is out of the scope of this answer.

This can be applied also to kglr's answer, asked for in the comments how to rotate labels:

BarLegend[{rbc, {0, 1}},
Ticks -> {{#, Rotate[Style["Hot", rbc[#]], 90 Degree]} &@
0, {#, Rotate[Style["Cold", rbc[#]], 90 Degree]} &@1},
LabelStyle -> {FontSize -> 13, Black}, LegendMarkerSize -> 236]


Produces:

• woow, that is perfect. is it possibple to rotate (90 degree) the labels such that the two words "Hot" and "cold " are aligned along the color bar? Apr 13, 2020 at 9:45
• Do you want a BarLegend that is a Row instead of the default Column one? Apr 13, 2020 at 9:46
• No, only the labels. Apr 13, 2020 at 9:46
• Sure! Just wrap them in Rotate and specify your Degree of rotation! I'll post an update. Apr 13, 2020 at 9:48
• cool, thanks a lot! Apr 13, 2020 at 9:49