3
$\begingroup$

I have this code to create some DensityPlot with M12.0.0.0 on Ubuntu

With[{z0 = 1, zm = 1}, 
 lgnds = BarLegend[{{Red, White, Blue}, {-zm, z0}}, 
   Ticks -> {{-zm + (zm/10), 
      Style[Text[-zm], Black, FontSize -> 14, 
       FontFamily -> "Latin Modern Roman"]}, {z0, 
      Style[Text[z0], Black, FontFamily -> "Latin Modern Roman", 
       FontSize -> 14]}}, 
   LegendMarkerSize -> 140, {LegendLayout -> "Row", 
    Charting`TickSide -> Right, TicksStyle -> None}]; 
 plot = Legended[
   DensityPlot[Sin[x y], {x, -2, 2}, {y, 0, 4}, 
    ColorFunction -> (Blend[{Red, White, Blue}, 
        Rescale[#, {-zm, z0}]] &), PlotPoints -> 80, 
    FrameStyle -> Black, FrameTicksStyle -> Black, PlotRange -> Full, 
    AspectRatio -> Full, ImageSize -> Automatic -> {220, 390}, 
    PlotRangeClipping -> False, 
    ImagePadding -> {{Automatic, Automatic}, {Automatic, 30}}, 
    FrameLabel -> {"", "Y"}, 
    LabelStyle -> {FontFamily -> "Latin Modern Roman", Black, 
      FontSize -> 16}, GridLines -> {{20.5}, None}, 
    GridLinesStyle -> Directive[Black, Dashed], 
    ColorFunctionScaling -> False, PlotRangePadding -> None], 
   Placed[lgnds, {{1, 1}, {1.15, 6.5}}]]]  

and this what I see in Mathematica where the BarLegend is correct

enter image description here

Now, I export this using Export["/home/.../plot.png", plot,ImageResolution -> 500] and this is what I got:

enter image description here

So, how can I keep the BarLegend position in the correct place when exporting?

$\endgroup$
6
  • $\begingroup$ Placed[lgnds, Bottom] $\endgroup$
    – cvgmt
    Commented Dec 14, 2020 at 14:04
  • $\begingroup$ @cvgmt I want to place it at a specific position below the figure and Bottom does not do the job. $\endgroup$
    – MMA13
    Commented Dec 14, 2020 at 14:08
  • 3
    $\begingroup$ I can reproduce this problem with 12.0.0 on macOS, but not with 12.1.1. $\endgroup$
    – Szabolcs
    Commented Dec 14, 2020 at 17:29
  • 1
    $\begingroup$ With 12.1.1 on Windows 10, I see the legend incorrectly placed (on top of the plot) in both Mathematica and in the exported png. $\endgroup$
    – MelaGo
    Commented Dec 16, 2020 at 23:43
  • $\begingroup$ @MelaGo, it is working in wind. but you need to change Placed[lgnds, {{1, 0.86}, {1.15, 6.5}}] $\endgroup$
    – MMA13
    Commented Dec 17, 2020 at 0:08

2 Answers 2

1
$\begingroup$

Well, the solution to that seem trivial-:) all we need to do is to replace {1,1} by Bottom in the BarLegend[]. So if we replace this in the original code Placed[lgnds, {Bottom, {0.42, 0.3}}] the figure in MMA and the exported one are the same.

With[{z0 = 1, zm = 1}, 
 lgnds = BarLegend[{{Red, White, Blue}, {-zm, z0}}, 
   Ticks -> {{-zm + (zm/10), 
      Style[Text[-zm], Black, FontSize -> 14, 
       FontFamily -> "Latin Modern Roman"]}, {z0, 
      Style[Text[z0], Black, FontFamily -> "Latin Modern Roman", 
       FontSize -> 14]}}, 
   LegendMarkerSize -> 140, {LegendLayout -> "Row", 
    Charting`TickSide -> Right, TicksStyle -> None}];
 plot = Legended[
   DensityPlot[Sin[x y], {x, -2, 2}, {y, 0, 4}, 
    ColorFunction -> (Blend[{Red, White, Blue}, 
        Rescale[#, {-zm, z0}]] &), PlotPoints -> 80, 
    FrameStyle -> Black, FrameTicksStyle -> Black, PlotRange -> Full, 
    AspectRatio -> Full, ImageSize -> Automatic -> {220, 390}, 
    PlotRangeClipping -> False, 
    ImagePadding -> {{Automatic, Automatic}, {Automatic, 30}}, 
    FrameLabel -> {"", "Y"}, 
    LabelStyle -> {FontFamily -> "Latin Modern Roman", Black, 
      FontSize -> 16}, GridLines -> {{20.5}, None}, 
    GridLinesStyle -> Directive[Black, Dashed], 
    ColorFunctionScaling -> False, PlotRangePadding -> None], 
   Placed[lgnds, {Bottom, {0.42, 0.3}}]]]   

and this is how it looks in MMA

enter image description here

and exporting this gives the same plot also

enter image description here

$\endgroup$
0
$\begingroup$
With[{z0 = 1, zm = 1}, 
 lgnds = BarLegend[{{Red, White, Blue}, {-zm, z0}}, 
   Ticks -> {{-zm + (zm/10), 
      Style[Text[-zm], Black, FontSize -> 14, 
       FontFamily -> "Latin Modern Roman"]}, {z0, 
      Style[Text[z0], Black, FontFamily -> "Latin Modern Roman", 
       FontSize -> 14]}}, 
   LegendMarkerSize -> 140, {LegendLayout -> "Row", 
    Charting`TickSide -> Right, TicksStyle -> None}]; 
 plot = Legended[
   DensityPlot[Sin[x y], {x, -2, 2}, {y, 0, 4}, 
    ColorFunction -> (Blend[{Red, White, Blue}, 
        Rescale[#, {-zm, z0}]] &), PlotPoints -> 80, 
    FrameStyle -> Black, FrameTicksStyle -> Black, PlotRange -> Full, 
    AspectRatio -> Full, ImageSize -> Automatic -> {220, 390}, 
    PlotRangeClipping -> False, 
    ImagePadding -> {{Automatic, Automatic}, {Automatic, 30}}, 
    FrameLabel -> {"", "Y"}, 
    LabelStyle -> {FontFamily -> "Latin Modern Roman", Black, 
      FontSize -> 16}, GridLines -> {{20.5}, None}, 
    GridLinesStyle -> Directive[Black, Dashed], 
    ColorFunctionScaling -> False, PlotRangePadding -> None], 
   Placed[lgnds, {{1, 0.86}, {1.15, 6.5}}]]]  

plot2

Specifically:

Placed[lgnds, {{1, 0.86}, {1.15, 6.5}}]

I modified your Placed lgnds location...this exports this image with your bar below the image, like you want it. I also run v12 on osx. If I remember correctly, ImagePadding[] is less than trivial and is likely what was causing the issue rather than Placed[]....I have had mannnny a broken image because of that function, with endless frustration...however I hope this fix, is quicker.

$\endgroup$
6
  • $\begingroup$ Indeed that will work but it does not solve the problem since the plot in the Notebook is not the same as the exported one $\endgroup$
    – MMA13
    Commented Dec 14, 2020 at 23:23
  • $\begingroup$ @HD2006 which version are you running? When i use your code, the plot in MMA was also broken. $\endgroup$ Commented Dec 14, 2020 at 23:24
  • $\begingroup$ M12.0.0.0 on Ubuntu $\endgroup$
    – MMA13
    Commented Dec 14, 2020 at 23:24
  • $\begingroup$ Bizarre...It may be a ubuntu thing? ...Also have you tried maybe just shutting down and restarting the kernal and running JUST that code you posted? I find when wonky things happen...it's because of something messing it up I forgot about. $\endgroup$ Commented Dec 14, 2020 at 23:26
  • $\begingroup$ I would also comment, the request was fixing the exported image, not your view in mma ;) $\endgroup$ Commented Dec 14, 2020 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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