# How can I precisely control the position of the BarLegend in the DensityPlot?

I generated the following plot and I would like to move the BarLegend to a specific position, how can I do that? I also faced another problem where in the BarLegend and close to the Tick on the blue side there is extra white space (marked by the green circle) that I could not get rid of?

With[{z0 = 0.1, zm = 0.1},
lgnds = BarLegend[{{Red, White, Blue}, {-zm, z0}},
Ticks -> {{-zm, Style[Text[-zm], 13, Black]}, {z0,
Style[Text[z0], 13, Black]}}, LegendMarkerSize -> 120,
LegendLabel ->
Placed[Style[ZZ, Red, FontSize -> 22], Left], {LegendLayout ->
"Row", ChartingTickSide -> Left}];
Legended[DensityPlot[x y, {x, -1, 1}, {y, -1, 1},
FrameStyle -> Black, FrameTicksStyle -> Black,
ColorFunction -> (Blend[{Red, White, Blue},
Rescale[#, {-zm, z0}]] &), PlotPoints -> 80, PlotRange -> Full,
FrameLabel -> {x, y}, ColorFunctionScaling -> False],
Placed[lgnds, {Top, Right}]]]


• Specify the position numerically, e.g., Placed[lgnds, {0.8, 1}] Sep 14 '20 at 15:02
• @BobHanlon, I tried that but still there is a big space between the top side of the plot-frame and the BarLegend Sep 14 '20 at 15:08

You can change the second argument of Placed in your code; that is, use

Placed[lgnds, {ImageScaled @ {1, 1}, {.92, .2}}]


Yo can also use Placed[lgnds, {{1.04, 1}, {1, .2}}] to get the same picture.

• that is great, just one more question please, what if I use ImageSize -> Automatic -> {150, 300} inside the DensityPlot, is it possible to use the same trick? Sep 14 '20 at 18:50
• @HD2006, the first approach does not for the case with ImageSize -> Automatic -> {150, 300}. Second approach works with appropriate choice of numbers. E.g., Placed[lgnds, {{1, .725}, {.94, .2}}].
– kglr
Sep 14 '20 at 19:30
• it is not working with me, may you please update the answer i updated the question as well. Sep 14 '20 at 19:36
• @HD2006, can you try Placed[lgnds, {{1, 1}, {.94, .2}}] and add the option AspectRatio->Full?
– kglr
Sep 14 '20 at 19:59
• now everything is perfect. Thanks a lot Sep 14 '20 at 20:06

EDIT: Corrected ImagePadding on right

With[
{z0 = 0.1, zm = 0.1},
DensityPlot[
x y,
{x, -1, 1}, {y, -1, 1},
FrameStyle -> Black,
FrameTicksStyle -> Black,
ColorFunction -> (Blend[{Red, White, Blue},
Rescale[#, {-zm, z0}]] &),
PlotPoints -> 80,
PlotRange -> Full,
FrameLabel -> {x, y},
ColorFunctionScaling -> False,
Epilog ->
Inset[
BarLegend[
{{Red, White, Blue}, {-zm, z0}},
Ticks -> {
{-zm, Style[Text[-zm], 13, Black]},
{z0, Style[Text[z0], 13, Black]}},
LegendMarkerSize -> 120,
LegendLabel -> Placed[
Style[ZZ, Red, FontSize -> 22],
Left],
LegendLayout -> "Row",
ChartingTickSide -> Left],
{0.58, 1.17}],
PlotRangeClipping -> False,
{{Automatic, Scaled[0.02]}, {Automatic, Scaled[0.075]}}]]


I've had trouble with this, so I usually Rasterize my graphics, then Inset my legend into my graphic. It will take some tweaking, but Inset gives you basically unlimited control. See below.

With[{z0 = 0.1, zm = 0.1},
legend =
ListDensityPlot[{Range[-0.1, 0.1, 0.1], Range[-0.1, 0.1, 0.1]},
DataRange -> All, ColorFunction -> (Blend[{Red, White, Blue},
Rescale[#, {-zm, z0}]] &), PlotRange -> Full,
ColorFunctionScaling -> False, AspectRatio -> 1/10,
FrameTicks -> {{None, None}, {None, {{1, Style[Text[-zm], 13, Black]},
{3, Style[Text[z0], 13, Black]}}}}, PlotRangePadding -> None];

plot = Rasterize@
DensityPlot[x y, {x, -1, 1}, {y, -1, 1}, FrameStyle -> Black,
FrameTicksStyle -> Black, ColorFunction -> (Blend[{Red, White, Blue},
Rescale[#, {-zm, z0}]] &), PlotPoints -> 80, FrameLabel -> {x, y},
ColorFunctionScaling -> False,PlotLabel -> Style["ZZ", Red, FontSize -> 22]];

Show[
plot,
Graphics@{Inset[legend, Scaled@{0.96, 0.93}, Scaled@{1, 1}, 125]},
PlotRange -> {{-15, 375}, {-15, 400}}]
]


• why did you remove LegendLabel -> Placed[Style[ZZ, Red, FontSize -> 22], Left]? Sep 14 '20 at 15:22
• It was an accident when I was cutting and pasting your code lol. I added it back with PlotLabel, I'm not positive where you want it placed but you can use Inset here as well. Sep 14 '20 at 15:23