# 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. –  David Park 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. –  LiaChica Jul 26 '13 at 8:04

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! –  LiaChica Jul 26 '13 at 13:35

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

-