5
$\begingroup$

I'd like to set the color range for a set of ContourPlots within a Table.

Please notice that the colors in the uploaded picture aren't the same from one plot to the other, (take a look to the interval [0,2] for example), even worse, some colors (like the yellow one in the 2nd and 3rd plots) are missing sometimes:

enter image description here

The code I'm using is this one:

Table[Show[{
ContourPlot[
  (4(1+10^r*Y^2(-94+255 X^2+178*X*Y-77 Y^2+7(5^r)*2^(1+r)*(5X-47Y)(29X-11Y)Y^2)))/(2+2^(2+r)*(5^r)Y^2(5-83*Y^2+2289*2^(1+r)*(5^r)*Y^4)), 
  {X, -1.3, 1.3}, {Y, -1.3, 1.3}, 
  ColorFunction -> "TemperatureMap", PlotRange -> {-7, 7}, 
  PlotLegends -> BarLegend[Automatic], 
  RegionFunction -> Function[{X, Y}, 
  (1/2)(1-(X+Y)^2+2^(1+r)*(5^r)*Y^2(-5X^2+94*X*Y+16*Y^2))>=0]
]}, RotateLabel -> False, FrameLabel -> {"X", "Y"}], {r, {-1.25, -0.625, 0}}]

EDIT: As suggested, I added a few lines of code BUT the results aren't nice yet, take a look:

Results with: ColorFunctionScaling -> False

enter image description here

(* 1st row: Modified lines *)
ColorFunction -> (ColorData["TemperatureMap", 
    Rescale[#, {-7, 7}]] &), ColorFunctionScaling -> False, PlotLegends -> BarLegend[{Automatic, {-7, 7}}],

(* 2nd row: Modified lines *)
ColorFunction -> (ColorData["TemperatureMap", 
    Rescale[#, {-7, 7}]] &), ColorFunctionScaling -> False, PlotLegends -> BarLegend[Automatic],

(* 3rd row: Modified lines *)
ColorFunction -> "TemperatureMap", ColorFunctionScaling -> False, PlotLegends -> BarLegend[{Automatic, {-7, 7}}],

(* 4th row: Modified lines *)
ColorFunction -> "TemperatureMap", ColorFunctionScaling -> False, PlotLegends -> BarLegend[Automatic],

Results without: ColorFunctionScaling -> False

enter image description here

(* 1st row: Modified lines *)
ColorFunction -> (ColorData["TemperatureMap", 
    Rescale[#, {-7, 7}]] &), PlotLegends -> BarLegend[{Automatic, {-7, 7}}],

(* 2nd row: Modified lines *)
ColorFunction -> (ColorData["TemperatureMap", 
    Rescale[#, {-7, 7}]] &), PlotLegends -> BarLegend[Automatic],

(* 3rd row: Modified lines *)
ColorFunction -> "TemperatureMap", PlotLegends -> BarLegend[{Automatic, {-7, 7}}],

(* 4th row: Modified lines *)
ColorFunction -> "TemperatureMap", PlotLegends -> BarLegend[Automatic],

I'll leave the question open for new comments and suggestions.

$\endgroup$
  • $\begingroup$ I don't think you want to use the 3rd row. See my updated answer. $\endgroup$ – Michael E2 Jul 22 at 4:55
7
$\begingroup$

The automatic legending gets fed the actual plot range, not the one you specified. So you have to specify the plot range in the BarLegend (plus the manual color function scaling, which must match BarLegend):

Table[Show[{
   ContourPlot[(4 (1 + 
         10^r*Y^2 (-94 + 255 X^2 + 178*X*Y - 77 Y^2 + 
            7 (5^r)*2^(1 + r)*(5 X - 47 Y) (29 X - 11 Y) Y^2)))/(2 + 
       2^(2 + r)*(5^r) Y^2 (5 - 83*Y^2 + 
          2289*2^(1 + r)*(5^r)*Y^4)), {X, -1.3, 1.3}, {Y, -1.3, 1.3},
    ColorFunction -> (ColorData["TemperatureMap", Rescale[#, {-7, 7}]] &),
    ColorFunctionScaling -> False,
    PlotLegends -> BarLegend[{Automatic, {-7, 7}}],
    PlotRange -> {-7, 7},
    RegionFunction -> Function[{X, Y}, (1/2) (1 - (X + Y)^2 + 
          2^(1 + r)*(5^r)*Y^2 (-5 X^2 + 94*X*Y + 16*Y^2)) >= 0]]},
  RotateLabel -> False, FrameLabel -> {"X", "Y"}],
 {r, {-1.25, -0.625, 0}}]

enter image description here

Update:

I didn't include the plots my code produced, because the OP's plots had odd stream lines that the OP's code did not produce. I thought I'd wait to see how my answer was received first. Now that the question has been updated, I can respond to this statement:

The 3rd row of these "Results without: ColorFunctionScaling -> False" I've showed you, gave me the desired result.

In third row, the contour levels and the scale in the bar legend are inconsistent, as one can see by hovering the mouse over the contour lines. This is typical of the case where ContourPlot automatically scales the colors in the plot and the user scales the colors in the legend. It's rare they coincide.

$\endgroup$
  • $\begingroup$ You're right Michael, I wrote it without noticing it. Do you know what is weird?, when I try your lines of code I see the same result as in the 1st row of "Results with: ColorFunctionScaling -> False". What could be wrong? - I'm using Mathematica 10 $\endgroup$ – JuanC97 Jul 22 at 5:37
  • $\begingroup$ @JuanC97 Nothing occurs to me. You can try Legended[ContourPlot[...], BarLegend[{"TemperatureMap", {-7, 7}}, Range[-6, 6, 2]]], which should prevent ContourPlot from messing with the BarLegend. $\endgroup$ – Michael E2 Jul 22 at 6:01
  • $\begingroup$ Now it works perfectly fine :) , thx Michael, I'm not sure why but maybe something in my kernel was causing a few problems. After reseting the kernel I'm able to reproduce your results. $\endgroup$ – JuanC97 Jul 22 at 22:33
  • $\begingroup$ Oh, and I also added Lengend[...] at the beginning, so... that helped too. Thx again ^^. $\endgroup$ – JuanC97 Jul 22 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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