# Erroneous color function for ContourPlot when the function has a small positive region and the PlotRange is specified manually

Bug introduced in 9.0 or earlier and persisting through 11.0.1 or later

Consider this ContourPlot

ContourPlot[.05 - .005 (x^2 + y^2), {x, -12, 12}, {y, -12, 12},
PlotRange -> {-2, 2},
Contours -> 20,
PlotLegends -> Automatic]


Clearly the color bar is wrong, assigning the region with the highest value to have the same color as the lowest value.

This problem goes away if you set the PlotRange-> All option, or if you set PlotRange-> {-2,x} where x is any number lower than 0.31 (e.g. 0.309999).

What is causing this behavior? Is it a bug?

• You haven't posted the code that produces the plot. – Ymareth Jan 25 '16 at 8:40
• @eli-morris, without the values of dataSlice3Adj we can't really try to reproduce the plot. What happens when you replace PlotRange -> {-2, 2} with PlotRange -> All and replace PlotLegends -> BarLegend[Automatic, All] with PlotLegends -> Automatic? – Jason B. Jan 25 '16 at 9:10
• Sorry. I just put in all the data, although, it's a little in the wrong order for evaluation. I guess I was thinking that the Mathematica answer was so off, it might be wrong in a way that someone could catch on to it as is. I can't image really any plot where both -1.5 and 0 would be basically the same color in the plot with a bunch of different colors in between Just weird, imho. Thanks for the help! – Eli Morris Jan 25 '16 at 9:18
• @EliMorris, it is almost letting me plot, but is missing definitions for AdjustOriginX, CenterY, and NormalizeValues. Basically, if we can get it so we can reproduce the problem you are having, we can see why it's doing that. But if we can't, then it's pretty hard to help – Jason B. Jan 25 '16 at 9:22
• The problem can be reproduced using e.g. 0.05 - 0.005 (x^2 + y^2) as the function to be plotted. – Simon Woods Jan 25 '16 at 12:45

I certainly think this is a bug, but you can get around it by using a custom ColorFunction, turning off ColorFunctionScaling, manually setting the range of the legend, and finally by manually setting the contour levels. This is the only way to ensure that you can compare multiple contour or density plots (I also use this method when I am trying to make an animation, so the color scale and contour levels can't change between frames.

colorfunc = ColorData[{"M10DefaultDensityGradient", {-2.0, 2.0}}];
contours = Subdivide[-2.0, 2.0, 20];
ContourPlot[#, {x, -12, 12}, {y, -12, 12},
PlotRange -> {-2, 2},
Contours -> contours,
PlotLegends -> BarLegend[{colorfunc, {-2, 2}}],
ColorFunction -> colorfunc,
ColorFunctionScaling -> False,
ImageSize -> 400] & /@ {.05 - .005 (x^2 + y^2),
1.8 - .015 (x^2 + y^2), .01 (x^2 + y^2 - x y)}


I reported this problem to Wolfram and they confirmed that it is indeed a bug and that they have forwarded the problem to the development team so they can fix it.