5
$\begingroup$

This is a problem of Legends in density plot. As pointed out by this post. Generally we want to same height of legend bar and plot.

The code for the basic case is

ContourPlot[Sin[x y], {x, 0, 3 \[Pi]}, {y, 0, \[Pi]}, 
 PlotLegends -> Automatic, ImageSize -> {Automatic, 100}]

Now I want to compare 4 cases

enter image description here

The first two case is OK. First one specify horizontal width ImageSize->100; second one specify vertical height ImageSize->{Automatic, 100}

But sometimes we want accurate aspect ratio. However, the third one with AspectRatio->Automatic is totally wrong. But we can see the legend bar is exactly the same, it conform with horizontal width, thought it should be confirm with image height!!

The fourth one as suggested by xslittlegrass is a fix for third one. Give right ascpet ratio, and at the same time want to get a legend bar of right height. One thing to note is that, I have intentionaly choose AspecRatio->1/3, ImageSize->300, and you can see again the bar is exactly the same as previous three plots. That is 300/3=100, this number is what PlotLegend conforms with. However, suprisely, the height is still not exactly right, as pointed out by red lines in the image. I don't know what is wrong?

So finally, my question is: How to make PlotLegend the same height as Plot which is specified with horizontal image width? (Constrain horizontal width of each plot is useful when we have multi plot in a row)

$\endgroup$
0

2 Answers 2

3
$\begingroup$

If you use a number instead of Automatic for the AspectRatio option, it seems to work:

ContourPlot[Sin[x y], {x, 0, 3 Pi}, {y, 0, Pi}, Contours -> 5, 
 PlotLegends -> Placed[BarLegend[Automatic], Right], AspectRatio -> 1/2, ImageSize -> 200]

enter image description here

Since AspectRatio->Automatic simply determines the ratio by the plot range, one can make an easy fix if we want to use the automatic aspect ratio:

myContourPlot[f_, {x_, xmin_, xmax_}, {y_, ymin_, ymax_}, 
  opts : OptionsPattern[]] := Module[{ratio},
  ratio = (ymax - ymin)/(xmax - xmin);
  ContourPlot[f, {x, xmin, xmax}, {y, ymin, ymax}, 
   Evaluate[
    FilterRules[{opts, AspectRatio -> ratio}, Options[Plot]]]]
  ]

myContourPlot[Sin[x y], {x, 0, 3 π}, {y, 0, π}, Contours -> 5,
  PlotLegends -> Placed[Automatic, Right], ImageSize -> 400]

enter image description here

$\endgroup$
5
  • $\begingroup$ Hi, xslittlegrass. But I want the Automatic! $\endgroup$
    – matheorem
    May 27, 2016 at 2:59
  • $\begingroup$ I agree that this behavior of bar legend in Automatic is strange. After all for ContourPlot, the AspectRatio->Automatic simply determines the ratio by the plot range, which is just Pi/(3Pi) in your case. I will be easy to make a fix for that. $\endgroup$ May 27, 2016 at 3:09
  • $\begingroup$ passing AspectRatio explicitly using parameter range, that is a good idea! $\endgroup$
    – matheorem
    May 27, 2016 at 6:24
  • $\begingroup$ There is a issue. If the aspect ratio getting larger. the PlotLegend gets ugly. Try {x, 0, 10 \[Pi]}, {y, 0, \[Pi]} $\endgroup$
    – matheorem
    May 27, 2016 at 6:50
  • $\begingroup$ I have greatly modified my post. Have a look : ) $\endgroup$
    – matheorem
    May 27, 2016 at 7:39
1
$\begingroup$

However, the third one with AspectRatio->Automatic is totally wrong. But we can see the legend bar is exactly the same, it conform with horizontal width, thought it should be confirm with image height!!

I see this behavior quite logical: you have specified the horizontal width only, so the size of the legend is now determined by what you have specified. When (in the second example) you specify the height only, the size of the legend is determined by the height.

So finally, my question is: How to make PlotLegend the same height as Plot which is specified with horizontal image width?

Apparently the developer of PlotLegend simply hadn't such goal in mind, so when Plot is specified with horizontal image width you have no simple way to force the PlotLegend to be of the same height as Plot. But if you really need the latter you always can relax the first requirement and simply supply Plot with the height as in your second example:

ContourPlot[Sin[x y], {x, 0, 3 π}, {y, 0, π}, 
 PlotLegends -> Automatic, AspectRatio -> 2, ImageSize -> {Automatic, 200}]

plot

With this approach you still have the control over the width of the plot: by changing its height you change its width proportionally!

$\endgroup$
2
  • $\begingroup$ Hi, Alexey Popkov. I just found PlotLabel will spoil things. for example ContourPlot[Sin[x y], {x, 0, 3 π}, {y, 0, π}, PlotLegends -> Automatic, AspectRatio -> 2, ImageSize -> {Automatic, 200}, PlotLabel->"ppp"] $\endgroup$
    – matheorem
    May 31, 2016 at 15:43
  • $\begingroup$ @matheorem You can avoid this using Labeled instead of PlotLabel: Labeled[ContourPlot[Sin[x y], {x, 0, 3 π}, {y, 0, π}, PlotLegends -> Automatic, AspectRatio -> 2, ImageSize -> {Automatic, 200}], "ppp", Top]. $\endgroup$ Jun 1, 2016 at 6:53

Your Answer

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

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