BarLegend can't make spaced contours when contours number>11
makeContours[barLegend_BarLegend] /; (Length[barLegend] =!= 2) := barLegend
makeContours[barLegend_BarLegend] :=
Module[{colorScheme = barLegend[[1]], contourCount = barLegend[[2]]},
If[IntegerQ[contourCount] && contourCount > 11 &&
DataPaclets`ColorDataDump`colorSchemeNameQ[colorScheme],
ToExpression[
MakeBoxes[barLegend] /.
RasterBox[colors_, scale_] :>
RasterBox[List@*List @@@ ColorData[colorScheme] /@ Subdivide[contourCount], scale] /.
(InterpretationFunction :> _) -> (InterpretationFunction :> (# &))],
barLegend]]
makeContours@BarLegend["LakeColors", #] & /@ Range[8, 20] // Row
Although there are hidden options to the underlying function (Charting`iBarLegend
) that seem to address the required functionality, there does not seem to be any way to get the user-exposed function BarLegend
to react to them. The following did not change the presentation of the test cases you provided, and neither did attempts to pass some promising-sounding options directly through BarLegend
. For example the following did not change anything:
BarLegend["LakeColors", 12, "SmoothRange" -> False]
I suspect that there is no way to get the desired effect in current versions of Mathematica using straightforward options, though I could be wrong. And as Karsten's excellent answer shows, it is possible to force the issue by constructing the raster manually.
SetOptions[Charting`iBarLegend, ShowContours -> True]
{ColorFunctionScaling -> Automatic, ImageSize -> Automatic,
ImageSizeAction -> Automatic, RoundingRadius -> Automatic,
FrameStyle -> Automatic, Alignment -> Automatic,
Spacings -> Automatic, Scrollbars -> False, ScrollPosition -> {0, 0},
Background -> Automatic, ContentPadding -> True,
DefaultLabelStyle -> {}, "Orientation" -> "Column",
Charting`AxesLabelRotate -> True, Charting`AxisAnnotation -> None,
Charting`AxisFactor -> None, Charting`AxisAppearance -> Automatic,
Charting`AxisLabel -> None, Charting`LabelSide -> Automatic,
Charting`PadLabels -> Automatic, Charting`RotateLabels -> False,
Charting`TickAnnotations -> None, Charting`TickSide -> Automatic,
Charting`TickLabels -> Automatic, Charting`TickLengths -> Automatic,
Charting`TickMarkers -> Automatic, Charting`TickWrappers -> None,
Charting`RotateTicks -> 0, AxesLabel -> None, AxesStyle -> Automatic,
Frame -> True, FrameStyle -> Automatic,
LabelingFunction -> Automatic, Ticks -> Automatic,
TicksStyle -> Automatic, AspectRatio -> Automatic,
ScalingFunctions -> {Identity, Identity}, BaseStyle -> {},
"LegendMethod" -> "Fixed", "Density" -> "Continuous",
"SmoothRange" -> True, "AxesPriority" -> Automatic,
"Sorting" -> True, "ContourMethod" -> "Graphics",
"RenderMode" -> Automatic, "LegendItemLayout" -> Automatic,
"PinningPoint" -> None, "StyledContours" -> None,
"ShowContours" -> True, PlotTheme -> "Mathematica"}
It's probably obvious to people with more experience than I, but since it took me a while to figure out:
If you want to modify Karsten's excellent answer above to produce horizontal BarLegends you need to take three inputs, transpose the matrix used in RasterBox, and add the LegendLayout->Row option to the function call:
makeContours[barLegend_BarLegend] /; (Length[barLegend] =!= 3) := barLegend
makeContours[barLegend_BarLegend] :=
Module[{colorScheme = barLegend[[1]], contourCount = barLegend[[2]]},
If[IntegerQ[contourCount] && contourCount > 11 &&
DataPaclets`ColorDataDump`colorSchemeNameQ[colorScheme],
ToExpression[
MakeBoxes[barLegend] /.
RasterBox[colors_, scale_] :>
RasterBox[Transpose[List@*List @@@
ColorData[colorScheme] /@ Subdivide[contourCount]],
scale] /. (InterpretationFunction :> _) -> \
(InterpretationFunction :> (# &))], barLegend]]
makeContours@BarLegend["LakeColors", #, LegendLayout -> "Row"] & /@
Range[8, 20] // Row
One additional note. If in OS X you export one of these bar legends to pdf, the discrete color blocks will disappear and you'll be back to a smooth gradient. Frustrating! This is because of a bug in Mathematica's pdf export, described here along with the unfortunate solution: PDF exports of ArrayPlot's are fuzzy (OS X) .