# Spacing of contours in BarLegend (Ver. 10 vs 11)

I recently started using Version 11, and I encountered the following problem. I use this code to make a contour plot:

myfunct[x_, y_] := 1.64 Exp[-6 (x^3 + y)] + 0.02 Sin[8 x + y] + 0.02;
ContourPlot[
myfunct[x,y], {x, 0, 1}, {y, 0, 1},
PlotRange -> Full,
Contours -> Table[x^2, {x, 0, Sqrt[1.7], 0.05}],
ContourStyle -> Thin,
FrameStyle -> Directive[FontFamily -> "Arial", 18, Black],
PlotLegends -> BarLegend[
Automatic,
LabelStyle -> Directive[FontFamily -> "Arial", FontSize -> 18, Black],
LegendMarkerSize -> 250
],
ImageSize -> 400,
MaxRecursion -> 4
]


And here is the comparison between the two versions (I can't disclose the plotted function, but the one given in the code above has a similar behaviour):

The behaviour I'm interested in is the one from Version 10. The legend does not scale, neither do the ticks (0, 0.5, 1.0, 1.5), the contours however scale according to the positions I have chosen for them.

My question is how to force Mathematica 11 to use the old style? Is there possibly an undocumented option that can do it?

• To experiment with your code, it would be useful to have the definition of myfunc. Please edit your question to include that definition. – m_goldberg Jan 31 '17 at 1:40
• @m_goldberg I can't give you the function I plotted, but I included a similar function in the code. The idea is that there are features that need finer contours near 0 values. – mszynisz Jan 31 '17 at 2:21
• I'm good with that. – m_goldberg Jan 31 '17 at 2:44
• Just as a note: when asking a question here, you don't necessarily have to give your specific function/data, but you do need to give a similar/simpler function/data that exhibits the problem you want solved, if you want other people to be able to try solving your problem. – J. M.'s ennui Jan 31 '17 at 3:59

I can reproduce the behavior you report but I don't think it's a bug. I think it's an improvement. Why? Because, in V11, you are getting a more accurate bar legend. The contour marks and labels are at values taken from actual contours in the plot. In V10, they were an interpolation of the contour values.

myfunct[x_, y_] := 1.64 Exp[-6 (x^3 + y)] + 0.02 Sin[8 x + y] + 0.02

ContourPlot[myfunct[x, y], {x, 0, 1}, {y, 0, 1},
PlotRange -> Full,
Contours -> Table[x^2, {x, 0, Sqrt[1.7], 0.05}],
FrameStyle -> Directive[FontFamily -> "Arial", 18, Black],
PlotLegends ->
BarLegend[Automatic,
LabelStyle ->
Directive[
FontFamily -> "Arial", FontSize -> 18, Black], LegendMarkerSize -> 250],
ImageSize -> 400,
MaxRecursion -> 4]


You specified the contours with

Table[x^2, {x, 0, Sqrt[1.7], 0.05}]


{0., 0.0025, 0.01, 0.0225, 0.04, 0.0625, 0.09, 0.1225, 0.16, 0.2025, 0.25, 0.3025, 0.36, 0.4225, 0.49, 0.5625, 0.64, 0.7225, 0.81, 0.9025, 1., 1.1025, 1.21, 1.3225, 1.44, 1.5625, 1.69}

Note that all the bar chart labels values appear in the above list, which means you are getting a much more accurate bar legend than you got in V10.

• Yes, I don't believe it's a bug either, it's actually more consistent with the BarLegend documentation which says the contours are equally spaced in the legend. However, I need the old behaviour for many of my plots, that's why I'm wondering if one can easily adjust e.g. undocumented option of the BarLegend? – mszynisz Jan 31 '17 at 9:28
• @mszynisz. I don't know any easy way. Certainly, you will have to specify something other than Automatic for the 1st argument of BarLegend -- Automatic means "I'll take what Mathematica gives me". But your question doesn't specify what you want in place of Automatic, so I don't have any ideas. ("I want V10 behavior" is not specific enough, at least for me) – m_goldberg Jan 31 '17 at 12:55

I managed to get the old behaviour by just adding two lines to the code:

myfunct[x_, y_] := 1.64 Exp[-6 (x^3 + y)] + 0.02 Sin[8 x + y] + 0.02;

ContourPlot[
myfunct[x, y], {x, 0, 1}, {y, 0, 1},
PlotRange -> Full,
Contours -> Table[x^2, {x, 0, Sqrt[1.7], 0.05}],
ContourStyle -> Thin,
FrameStyle -> Directive[FontFamily -> "Arial", 18, Black],
PlotLegends -> BarLegend[
Automatic, None, (* added due to MinHsuanPeng *)
LabelStyle ->
Directive[FontFamily -> "Arial", FontSize -> 18, Black],
LegendMarkerSize -> 250,
"StyledContours" -> Table[{x^2, Opacity[0.5]}, {x, 0, Sqrt[1.7], 0.05}] (* added *)
],
ImageSize -> 400,
MaxRecursion -> 4
]


And the output is basically identical to the Ver. 10 plot:

• BarLegend[Automatic, None, your_options] actually will do the honor for you. – MinHsuan Peng Jan 31 '17 at 18:23
• @MinHsuanPeng Thanks! I'll add this to the code. – mszynisz Jan 31 '17 at 18:37