# How to color a contour plot over a subset of colors displayed in the bar legend

This seems real easy but I can't find the answer. I have a few contour plots that I am combining and want to use only one bar legend that captures all the values. Given that I know the range of values, how to get the colors to correspond so that, say, if one plot only ranges over 1/3 of the total range, it only displays 1/3 of the range of colors. The following code only plots the BarLegend correctly but leaves the colors in the contour plot unchanged:

ContourPlot[X^2+Y^2,{X,-3,3},{Y,-3,3},PlotLegends->BarLegend[{"LakeColors",{0, 100}}, 10]]


Edit: I should have been more precise. Here is a more explicit example. The goal is to make the contour colors quantitatively consistent between the plots.

h1 = ContourPlot[X^2 + Y^2, {X, -3, 3}, {Y, -3, 3}, PlotLegends -> Automatic];
h2 = ContourPlot[X^2 + Y^2, {X, -5, 5}, {Y, -5, 5}, PlotLegends -> Automatic];
GraphicsRow[{h1, h2}, ImageSize -> 500]


As @David_Park mentions, I need to use ColorFunction. But I don't know how with ContourPlot.

• For each of your plots use the option ColorFunctionScaling -> False. Then use the same ColorFunction option in each of the plots. Jul 26 '13 at 0:44
• This is what I am not clear on what to do. i.e. how to use the color function with contour plots. Jul 26 '13 at 8:04

From David's suggestion to use ColorFunctionScaling and Colorfunction, I realized that the default scaling of color functions is between 0 and 1, so they must be rescaled over the desired range. I add the legend manually at the end.

minVal = 0; maxVal = 50;
h1 = ContourPlot[X^2 + Y^2, {X, -3, 3}, {Y, -3, 3},
ColorFunctionScaling -> False,
ColorFunction -> (ColorData["LakeColors"][
Rescale[#, {minVal, maxVal}]] &)];
h2 = ContourPlot[X^2 + Y^2, {X, -5, 5}, {Y, -5, 5},
ColorFunctionScaling -> False,
ColorFunction -> (ColorData["LakeColors"][
Rescale[#, {minVal, maxVal}]] &)];
Legended[GraphicsRow[{h1, h2}, ImageSize -> 500],
BarLegend[{"LakeColors", {minVal, maxVal}}]]


Since your contours range only up to 18, limit the color range to 18 in the specification of BarLegend.

ContourPlot[X^2 + Y^2, {X, -3, 3}, {Y, -3, 3},
PlotLegends -> BarLegend[{"LakeColors", {0, 18}}]] Or perhaps

ContourPlot[X^2 + Y^2, {X, -3, 3}, {Y, -3, 3},
PlotLegends -> BarLegend[{"LakeColors", {0, 18}}, Range[0, 18, 2]]]


if you want to explicitly set the ticks on the legend. • I wasn't clear enough I guess in saying that I want to specify the range of colors used in the legend independently of the range of colors in the plot. I figured it out and posted the answer. Thanks, though! Jul 26 '13 at 13:35

Yes, you did that correctly. But I'm making a guess that there is another underlying consideration in your question. Sometimes we have a function that has a rather large plateau in the middle of the plot. If we just use regular Mathematica generated contours this will just show as a single solid colored region. Specifying custom Contours with a finer graduation in the plateau region helps but then the contour regions will all be about the same color.

The Presentations application has a ContourColors ColorFunction that uses the list of Contours and generates distinct colors for each region. I'm not going to show the details but just a result from one example. In this case the legends are actually also ContourPlots and I've placed all three plots on one "piece of paper". One legend is for the overall plot and the second legend is for the plateau region. • How did you do that? Those two bars and the correct scaling ?
– Nick
Jun 9 '14 at 10:40