# How to make height of PlotLegend the same as height of plot which has a fixed horizontal width

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

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)

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]


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]


• Hi, xslittlegrass. But I want the Automatic! May 27 '16 at 2:59
• 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. May 27 '16 at 3:09
• passing AspectRatio explicitly using parameter range, that is a good idea! May 27 '16 at 6:24
• There is a issue. If the aspect ratio getting larger. the PlotLegend gets ugly. Try {x, 0, 10 \[Pi]}, {y, 0, \[Pi]} May 27 '16 at 6:50
• I have greatly modified my post. Have a look : ) May 27 '16 at 7:39

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


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

• 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"] May 31 '16 at 15:43
• @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]. Jun 1 '16 at 6:53