Whenever I try to fit the sizes of the plot and the BarLegend
, it's a struggle.
If one sets the same ImageSize
, it's ok as long as one does not play with the ImagePadding
.
Labeled[DensityPlot[Sin[x y], {x, -Pi, Pi}, {y, -Pi, Pi},
ImageSize -> 300, ColorFunction -> "SunsetColors"],
BarLegend[{"SunsetColors", {-1, 1}}, LegendLayout -> "Row",
ImageSize -> 300, LegendMarkerSize -> 300], {{Top, Center}}]
When creating columns/rows of plots, it is crucial to modify the default ImagePadding
to fix the size of all plots that I create. But then I do not know how to fit the size of the BarLegend
without trial and error - the way its margins work is a mystery (LegendMargins
is not really a great option - I find it very frustrating to work with).
Labeled[DensityPlot[Sin[x y], {x, -Pi, Pi}, {y, -Pi, Pi},
ImageSize -> 300, ColorFunction -> "SunsetColors",
ImagePadding -> {{50, 2}, {2, 2}}],
BarLegend[{"SunsetColors", {-1, 1}}, LegendLayout -> "Row",
ImageSize -> 300, LegendMarkerSize -> 250], {{Top, Center}}]
Would greatly appreciate your advice on how to tackle this issue. I'm aware that there were questions about this problem before, but the answers do not really help.
{{Top, Center}}
to{{Top, Right}}
? $\endgroup$ImagePadding
forBarLegend
and theLegendMargins
option is, imho, not really working well. $\endgroup$ipad = {{50, 8}, {2, 2}}; Legended[DensityPlot[Sin[x y], {x, -Pi, Pi}, {y, -Pi, Pi}, ImageSize -> 300, ColorFunction -> "SunsetColors", ImagePadding -> ipad, PlotRangeClipping -> False], Placed[BarLegend[{"SunsetColors", {-1, 1}}, LegendLayout -> "Row", Method -> {ImageSize -> 300, ImagePadding -> {{0, 0}, {20, 0}} + ipad}], {Above, Right}]]
works in your version/os. It works in version 9 / windows 10. $\endgroup$DensityPlot[Sin[x y], {x, -Pi, Pi}, {y, -Pi, Pi}, ImageSize -> 300, ColorFunction -> "SunsetColors", ImagePadding -> ipad, PlotRangeClipping -> False, PlotLegends -> Placed[BarLegend[{"SunsetColors", {-1, 1}}, LegendLayout -> "Row", Method -> {ImageSize -> 300, ImagePadding -> ipad + {{0, 0}, {0, 20}}}], {Above, Right}]]
also works in v9/w10. $\endgroup$ImagePadding
inside theMethod
option. $\endgroup$