Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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.


share|improve this answer
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
up vote 1 down vote accepted

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}}]]
share|improve this answer

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.

enter image description here

share|improve this answer
How did you do that? Those two bars and the correct scaling ? – Dominique Jun 9 '14 at 10:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.