How can I change the thickness of tick marks in BarLegend?
Somehow the AbsolutThickness
you specified gets replaced by a default value of AbsoluteThickness[0.2]
.
This misbehavior can be corrected by replacing the incorrect value with your specification.
PlotLegends; (*preload definitions*)
Cell[BoxData[
MakeBoxes@
BarLegend[{"SunsetColors", {0, 1}}, LabelStyle -> {FontSize -> 12},
Method -> {Frame -> False, TicksStyle -> Directive[Red, AbsoluteThickness[2]]}] /.
Directive[RGBColor[1, 0, 0], AbsoluteThickness[_]] ->
Directive[RGBColor[1, 0, 0], AbsoluteThickness[2]] // #[[1, 1]] &
], "Output"] // CellPrint
For opaque ticks:
Cell[BoxData[
MakeBoxes@
BarLegend[{"SunsetColors", {0, 1}}, LabelStyle -> {FontSize -> 12},
Method -> {Frame -> False, TicksStyle -> Directive[Red, AbsoluteThickness[2]]}] /.
Directive[RGBColor[1, 0, 0], AbsoluteThickness[_]] ->
Directive[RGBColor[1, 0, 0], AbsoluteThickness[2], Opacity[1]] // #[[1, 1]] &
], "Output"] // CellPrint
Correcting the BarLegend
of a DensityPlot
, using the syntax provided in the answer by Praan :
DensityPlot[Sin[x y], {x, 0, 1}, {y, 0, 1},
PlotLegends ->
BarLegend[Automatic, LabelStyle -> {FontSize -> 12},
Method -> {Frame -> False, TicksStyle -> Directive[Red, AbsoluteThickness[2]]}]] /.
Placed[barLegend_BarLegend, args__] :>
Placed[ToExpression[
FrameBox @@ MakeBoxes[barLegend] /.
Directive[Red, AbsoluteThickness[_]] ->
Directive[Red, AbsoluteThickness[2], Opacity[1]]], args]
The same output can be achieved by using the following LegendFunction
DensityPlot[Sin[x y], {x, 0, 1}, {y, 0, 1},
PlotLegends ->
BarLegend[Automatic, LabelStyle -> {FontSize -> 12},
Method -> {Frame -> False,
TicksStyle -> Directive[Red, AbsoluteThickness[2]]},
LegendFunction -> (# /.
Directive[Red, AbsoluteThickness[_]] ->
Directive[Red, AbsoluteThickness[2], Opacity[1]] &)]]
With the answer by Praan and our discussion in the comments it became clear, that a wrong InterpretationFunction
inside the TemplateBox
created by BarLegend
can cause additional problems.
Compare
MakeBoxes[
BarLegend[{"SunsetColors", {0, 1}}, LegendMarkerSize -> 300,
LabelStyle -> {FontSize -> 12},
Method -> {FrameStyle -> Black, AxesStyle -> None,
TicksStyle -> Black}]] /.
AbsoluteThickness[_] ->
AbsoluteThickness[2] /. (InterpretationFunction :>
f_) -> (InterpretationFunction :> (# &)) // ToExpression
with
MakeBoxes[
BarLegend[{"SunsetColors", {0, 1}}, LegendMarkerSize -> 300,
LabelStyle -> {FontSize -> 12},
Method -> {FrameStyle -> Black, AxesStyle -> None,
TicksStyle -> Black}]] /.
AbsoluteThickness[_] -> AbsoluteThickness[2] // ToExpression
or just the InterpretationFunction
MakeBoxes[
BarLegend[{"SunsetColors", {0, 1}}, LegendMarkerSize -> 300,
LabelStyle -> {FontSize -> 12},
Method -> {FrameStyle -> Black, AxesStyle -> None,
TicksStyle -> Directive[Black, AbsoluteThickness[2]]}]] /.
AbsoluteThickness[_] ->
AbsoluteThickness[2] // #[[-1, 2, 1]] & // ToExpression
and the first code block in the answer by Praan.
Update: You can also use the forms given in Options[Charting`iBarLegend]
listed below, i.e., we can use Charting`TicksStyle
for "TicksStyle"
, Charting`Frame
for "Frame"
, etc.
BarLegend[{"SunsetColors", {0, 1}}, LabelStyle -> {FontSize -> 12},
Charting`TicksStyle -> Directive[Opacity[1], Red, FontColor -> Black, AbsoluteThickness[5]],
Charting`Frame -> False,
Charting`TickLengths -> {20, 10}]
Note: This works for versions 9 thru 11 (Windows 10).
Original answer:
Use the (undocumented, afaik) options "TicksStyle"
and "FrameStyle"
:
BarLegend[{"SunsetColors", {0, 1}}, LabelStyle -> {FontSize -> 12},
"TicksStyle" -> Directive[Red, AbsoluteThickness[5]],
"FrameStyle" -> Opacity[0]]
In version 10, we need to add Opacity[1]
in Directive[...]
:
BarLegend[{"SunsetColors", {0, 1}}, LabelStyle -> {FontSize -> 12},
"TicksStyle" -> Directive[Opacity[1], Red, FontColor->Black, AbsoluteThickness[5]],
"Frame" -> False]
A few more such options:
BarLegend[{"SunsetColors", {0, 1}},
"LabelStyle" -> Directive[Green, Bold, FontSize -> Scaled[.4]],
"Ticks" -> Range[0, 1, .1],
"TicksStyle" -> Directive[Red, AbsoluteThickness[5]],
"FrameStyle" -> Directive[Dashed, Green], "TickSide" -> Left,
"LabelSide" -> Right, "Frame" -> True, "ImageSize" -> {100, 300},
LegendMarkerSize -> {100, 500}]
Update: Full list of options
Options[Charting`iBarLegend]
Version 9:
Version 10:
Analysis
(Observations in 10.1.0 under Windows.)
Curiously it seems that AbsoluteThickness[0.2`]
is hard-coded within the internal definitions. Formatting of BarLegend
calls Charting`iBarLegend
which calls Legending`LegendDump`iColorGradientLegend
or Legending`LegendDump`iColorBandLegend
. In the definition of iColorGradientLegend
we find:
ticksstyle = Switch[ticksstyle,
Automatic, {Directive[AbsoluteThickness[0.2`], GrayLevel[0.4`]],
Directive[AbsoluteThickness[0.2`], GrayLevel[0.4`], Opacity[0.3`]]},
_?Charting`generalDirective | {_?
Charting`generalDirective ..} | {}, (Charting`ConstructDirective[#1,
AbsoluteThickness[0.2`]] &) /@ Charting`padList[ticksstyle, 2],
None | False, None,
_, (Directive[AbsoluteThickness[0.2`], #1] &) /@ {GrayLevel[0.4`],
Directive[GrayLevel[0.4`], Opacity[0.5`]]}]
If the option value passed to this inner function is a "generalDirective" we specifically get:
(Charting`ConstructDirective[#1,
AbsoluteThickness[0.2`]] &) /@ Charting`padList[ticksstyle, 2]
So the program is appending AbsoluteThickness[0.2`]
to anything we specify by way of Charting`ConstructDirective
. This seems at best a poor design in my opinion.
Work-around
Seeing the cause of the problem inspires an unusual work-around; here is a chomp
head that consumes the argument to its right and then vanishes:
chomp /: h_[a___, chomp[x__], b_, c___] := h[a, x, c]
Now when used on our AbsoluteThickness
spec making sure it is the last:
BarLegend[{"SunsetColors", {0, 1}}, LabelStyle -> {FontSize -> 12},
Method -> {Frame -> False,
TicksStyle -> Directive[Red, Opacity[1], chomp @ AbsoluteThickness[4]]}]
One could also enter Directive[chomp[ Red, Opacity[1], AbsoluteThickness[4] ] ]
.