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.
Answer
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}}]]
Comments
Post a Comment