Como posso controlar com precisão a posição do BarLegend no DensityPlot?

Sep 14 2020

Gerei o seguinte gráfico e gostaria de mover o BarLegendpara uma posição específica, como posso fazer isso? Eu também enfrentei outro problema onde no BarLegende próximo ao do Ticklado azul há um espaço em branco extra (marcado pelo círculo verde) que eu não consegui me livrar?

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

Respostas

3 kglr Sep 15 2020 at 01:38

Você pode alterar o segundo argumento de Placedem seu código; ou seja, use

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

Você também pode usar Placed[lgnds, {{1.04, 1}, {1, .2}}]para obter a mesma imagem.

3 BobHanlon Sep 14 2020 at 22:35

Use EpilogeInset

EDITAR: corrigido ImagePaddingà direita

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

2 atlasgeo Sep 14 2020 at 22:17

Tive problemas com isso, então geralmente coloco Rasterizemeus gráficos, depois Insetminha legenda em meus gráficos. Vai demorar alguns ajustes, mas Insetoferece controle basicamente ilimitado. Veja abaixo.

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