2
$\begingroup$

I'm trying to compare two DensityPlots:

Module[{R1, R2, x, Δ},
 R1 = 15;
 R2 = 5;

 Row @ {
   DensityPlot[-((R1 + R2 + R1 x - R2 x - R2 Δ - R1 x Δ + R2 x Δ)/(-2 + Δ)),
    {x, 0, 1}, {Δ, 0, 1}, 
    ImageSize -> 700, FrameLabel -> {"% talented", "delta"}, 
    PlotLegends -> Automatic],
   DensityPlot[-((
     2 R2 + R1 x - R2 x - R2 Δ - R1 x Δ + R2 x Δ)/(-2 + Δ)),
     {x, 0, 1}, {Δ, 0, 1}, 
    ImageSize -> 700, FrameLabel -> {"% talented", "delta"}, 
    PlotLegends -> Automatic]
   }]

but it would be easier if the colors were coding for the same values in the two graphs. I was able to set the same range of values for the legend with:

PlotLegends -> Placed[BarLegend[{Automatic, {0, 15}}], Right]

but how do I change the command so that the colors of my graphs will represent the whole range of values from 0 to 15?

$\endgroup$

1 Answer 1

3
$\begingroup$

One approach could be to set the scaling of the colors yourself: (I slightly cleaned up the code a bit, so that we do not get confused by double-setting the options)

Module[{R1, R2, x, \[CapitalDelta], eqs},
 R1 = 15;
 R2 = 5;
 eqs = {-((R1 + R2 + R1 x - R2 x - R2 \[CapitalDelta] - 
        R1 x \[CapitalDelta] + 
        R2 x \[CapitalDelta])/(-2 + \[CapitalDelta])), -((2 R2 + 
        R1 x - R2 x - R2 \[CapitalDelta] - R1 x \[CapitalDelta] + 
        R2 x \[CapitalDelta])/(-2 + \[CapitalDelta]))};
 Row[DensityPlot[#, {x, 0, 1}, {\[CapitalDelta], 0, 1}, 
     ImageSize -> Medium, FrameLabel -> {"% talented", "delta"}, 
     PlotLegends -> Placed[BarLegend[{Automatic, {0, 15}}], Right], 
     ColorFunction -> (ColorData["SunsetColors"][#/15] &), 
     ColorFunctionScaling -> False] & /@ eqs
  ]]

I use ColorFunctionScaling->False to keep Mathematica from rescaling the colors. Note that I hardcoded the 15. You might want to adjust that.

Note: I kept your version of the legend.

EDIT, including ContourPlot

For ContourPlot, we have to define the legend differently (I guess this is due to the fact that Mathematica tries to build the legend around the existing contours of the graph, thus cuts off the rest of the legend - hence we apply some force)

 Module[{R1, R2, x, \[CapitalDelta], eqs, cf},
 R1 = 15;
 R2 = 5;
 cf = ColorData["SunsetColors"][#/15] &;
 eqs = {-((R1 + R2 + R1 x - R2 x - R2 \[CapitalDelta] - 
        R1 x \[CapitalDelta] + 
        R2 x \[CapitalDelta])/(-2 + \[CapitalDelta])), -((2 R2 + 
        R1 x - R2 x - R2 \[CapitalDelta] - R1 x \[CapitalDelta] + 
        R2 x \[CapitalDelta])/(-2 + \[CapitalDelta]))};
 Row[
  ContourPlot[#, {x, 0, 1}, 
  {\[CapitalDelta], 0, 1}, 
  ImageSize -> Medium, 
  FrameLabel -> {"% talented", "delta"}, 
  PlotLegends -> Placed[BarLegend[{cf, {0, 15}}, 30], Right],
  ColorFunction -> cf, ColorFunctionScaling -> False] & /@ eqs]]

There is probably a more direct way to implement that. The way I suggest here specifies the ColorFunction once more with the proper scaling and creates the legend based on that.

Note: this legend works for both DensityPlot and ContourPlot, it seems.

end of edit

The resulting image (DensityPlot) looks like (I like SunsetColors):

enter image description here

$\endgroup$
2
  • $\begingroup$ that's great. Any idea why the legend is messed up when I use ContourPlot instead of DensityPlot? $\endgroup$
    – Sulli
    Commented May 15, 2013 at 14:09
  • $\begingroup$ glad you like it - I added an edit to fix things for ContourPlot. I hope this works for you! $\endgroup$ Commented May 15, 2013 at 14:41

Your Answer

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

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