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]
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityYou 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]
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, 2020 at 9:26
BarLegend
.
$\endgroup$
"TickLabels"
be defined in the order that they're supplied to Ticks
? You could certainly beat me at codegolf with this!
$\endgroup$
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?!
$\endgroup$
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:
BarLegend
that is a Row
instead of the default Column
one?
$\endgroup$
Apr 13, 2020 at 9:46
Rotate
and specify your Degree
of rotation! I'll post an update.
$\endgroup$
Apr 13, 2020 at 9:48
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 usingStyle
to force the coloration of the labels. $\endgroup$