2
$\begingroup$

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.

$\endgroup$
2
  • 3
    $\begingroup$ For each of your plots use the option ColorFunctionScaling -> False. Then use the same ColorFunction option in each of the plots. $\endgroup$
    – David Park
    Jul 26, 2013 at 0:44
  • $\begingroup$ This is what I am not clear on what to do. i.e. how to use the color function with contour plots. $\endgroup$
    – LiaChica
    Jul 26, 2013 at 8:04

3 Answers 3

3
$\begingroup$

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}}]]
$\endgroup$
1
$\begingroup$

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

contours.png

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.

contour.png

$\endgroup$
1
  • $\begingroup$ 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! $\endgroup$
    – LiaChica
    Jul 26, 2013 at 13:35
0
$\begingroup$

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

$\endgroup$
1
  • 1
    $\begingroup$ How did you do that? Those two bars and the correct scaling ? $\endgroup$
    – Nick
    Jun 9, 2014 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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