6
$\begingroup$

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]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ 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. $\endgroup$ Apr 13 '20 at 9:44
8
$\begingroup$

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]

enter image description here

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]

enter image description here

and

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

enter image description here

and

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

enter image description here

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]

enter image description here

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$ Apr 13 '20 at 9:26
  • 1
    $\begingroup$ @CATrevillian, updated version shows a way to style the tick labels using the color function in the first argument of BarLegend. $\endgroup$
    – kglr
    Apr 13 '20 at 9:49
  • 1
    $\begingroup$ 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! $\endgroup$ Apr 13 '20 at 9:53
  • 1
    $\begingroup$ 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?! $\endgroup$ Apr 13 '20 at 14:43
6
$\begingroup$

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:

Image of BarLegend with rotated labels by 90 degrees.

$\endgroup$
6
  • $\begingroup$ 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? $\endgroup$ Apr 13 '20 at 9:45
  • $\begingroup$ Do you want a BarLegend that is a Row instead of the default Column one? $\endgroup$ Apr 13 '20 at 9:46
  • $\begingroup$ No, only the labels. $\endgroup$ Apr 13 '20 at 9:46
  • $\begingroup$ Sure! Just wrap them in Rotate and specify your Degree of rotation! I'll post an update. $\endgroup$ Apr 13 '20 at 9:48
  • 1
    $\begingroup$ cool, thanks a lot! $\endgroup$ Apr 13 '20 at 9:49

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.