2
$\begingroup$

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", Charting`TickSide -> 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}]]]  

enter image description here

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

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

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

enter image description here

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

$\endgroup$
5
  • $\begingroup$ 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? $\endgroup$ Sep 14 '20 at 18:50
  • $\begingroup$ @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}}]. $\endgroup$
    – kglr
    Sep 14 '20 at 19:30
  • $\begingroup$ it is not working with me, may you please update the answer i updated the question as well. $\endgroup$ Sep 14 '20 at 19:36
  • $\begingroup$ @HD2006, can you try Placed[lgnds, {{1, 1}, {.94, .2}}] and add the option AspectRatio->Full? $\endgroup$
    – kglr
    Sep 14 '20 at 19:59
  • 1
    $\begingroup$ now everything is perfect. Thanks a lot $\endgroup$ Sep 14 '20 at 20:06
3
$\begingroup$

Use Epilog and Inset

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",
     Charting`TickSide -> Left],
    {0.58, 1.17}],
  PlotRangeClipping -> False,
  ImagePadding ->
   {{Automatic, Scaled[0.02]}, {Automatic, Scaled[0.075]}}]]

enter image description here

$\endgroup$
2
$\begingroup$

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}}]
 ]

using Inset to add a legend to graphics object

$\endgroup$
2
  • $\begingroup$ why did you remove LegendLabel -> Placed[Style[ZZ, Red, FontSize -> 22], Left]? $\endgroup$ Sep 14 '20 at 15:22
  • $\begingroup$ 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. $\endgroup$
    – atlasgeo
    Sep 14 '20 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.