Size of BarLegend
Here's a workaround that might help others in the future: do not use BarLegend
and create your own bar using DensityPlot
. This way one gets the ImagePadding
option.
Here's an example that works perfectly.
Horizontal bar legend
myBarPlot =
DensityPlot[x, {x, -1, 1}, {y, -1, 1},
PlotRange -> {{-1, 1}, {-1, 1}}, ColorFunction -> "SunsetColors",
AspectRatio -> 1/15, FrameTicks -> {{None, None}, {True, None}},
ImagePadding -> {{50, 8}, {20, 2}}, ImageSize -> 300];
Column[{myBarPlot,
DensityPlot[Sin[x y], {x, -Pi, Pi}, {y, -Pi, Pi}, ImageSize -> 300,
ColorFunction -> "SunsetColors",
ImagePadding -> {{50, 8}, {2, 2}}]}, Spacings -> 0]
Vertical bar legend Second case, suggested by @OkkesDulgerci
myBarPlotVertical =
DensityPlot[y, {x, -1, 1}, {y, -1, 1},
PlotRange -> {{-1, 1}, {-1, 1}}, ColorFunction -> "SunsetColors",
AspectRatio -> 15, FrameTicks -> {{None, All}, {None, None}},
ImagePadding -> {{2, 25}, {5, 5}}, ImageSize -> {Automatic, 300}];
Row[{DensityPlot[Sin[x y], {x, -Pi, Pi}, {y, -Pi, Pi},
ImageSize -> {Automatic, 300}, ColorFunction -> "SunsetColors",
ImagePadding -> {{50, 8}, {5, 5}}, PlotRangePadding -> None],
myBarPlotVertical}]
Still, would be great to know, if there's an "official" way to do it using BarLegend
.
You gave a good answer. However, in addition to that, here is the one based on using the standard technique:
DensityPlot[Sin[x y], {x, -Pi, Pi}, {y, -Pi, Pi}, ImageSize -> 300,
ImagePadding -> {{50, 2}, {2, 2}}, ColorFunction -> "SunsetColors",
PlotLegends ->
Placed[Row[{Spacer[30],
BarLegend[{"SunsetColors", {-1, 1}}, LegendLayout -> "Row",
LegendMarkerSize -> 300 - 26]}], {Above, Center}]]
One can play with two parameters to adjust the size and position: (1) the argument of the Spacer as well as with (2) the value of the LegendMarkerSize.
Have fun!