BarLegend
Represents bar-type legend
BarLegend[cf_ColorFunction | cf_String]BarLegend[{cf_, {min, max}}]where cf is a color function (named or provided explicitly)
BarLegend[cf_, tickCount_Integer]limits the number of ticks shown on the bar legend.
Options
LegendLabel
Adds a label to the legend.
LabelStyle
Styles both the legend label and its tick labels. Colors, FontFamily, and FontSize are supported.
ImageSize
Sets the size of the color bar. A two-element list can be used to align the legend height with a plot.
LegendLayout
Use LegendLayout -> "Row" to display the color bar horizontally.
ColorFunctionScaling
By the deafult is True, which rescales values to the range from 0 to 1 and supply it to the provided color function (if provided)
Examples
Named colors
BarLegend[{"Rainbow", {0,1}}]Using Hue
BarLegend[{Hue, {0, 1}}]Implicit usage
Automatically generated and wrapped into Legended
ContourPlot[Sin[x y], {x, 0, \[Pi]}, {y, 0, \[Pi]}, Contours -> 5, PlotLegends -> Automatic]Label and tick count
BarLegend["Thermometer", 3, LegendLabel -> "Temperature"]Horizontal layout
Use a two-element ImageSize for a wide, short bar. A label can be given as
{label, {x, y}} to apply an absolute offset:
BarLegend[
{"Rainbow", {0, 1}},
2,
LegendLayout -> "Row",
ImageSize -> {300, 20},
LegendLabel -> {"Temperature", {0, -35}}
]Align with a plot
ContourPlot[
(x^2 - y^2)/(x^2 + y^2)^2,
{x, -1, 1}, {y, -1, 1},
Contours -> 4,
PlotLegends -> BarLegend[Automatic, ImageSize -> {20, 300}],
ImageSize -> {300, 300},
ColorFunction -> "Rainbow",
ClippingStyle -> Automatic
]Make the legend labels larger and limit the number of ticks:
ContourPlot[
(x^2 - y^2)/(x^2 + y^2)^2,
{x, -1, 1}, {y, -1, 1},
Contours -> 4,
PlotLegends -> BarLegend[Automatic, 3,
ImageSize -> {20, 300},
LabelStyle -> Directive[FontSize -> 14],
LegendLabel -> "f(x,y)"
],
ImageSize -> {300, 300},
ColorFunction -> "Rainbow",
ClippingStyle -> Automatic,
FrameStyle -> Directive[FontSize -> 14],
FrameTicksStyle -> Directive[FontSize -> 14],
FrameTicks -> {{-1, 0, 1}, {-1, 0, 1}}
]