2
$\begingroup$

I know questions with this theme have been asked before, and the one-line answer to them is-

"Use ColorFunctionScaling -> False"

But my specific concern is the same scaling for tweaked colour themes using the ColorBar package or some self-defined colour functions using the Blend option in Mathematica.

Let me provide an example below to explain my problem:

Suppose I have the following Mathematica code:

bl = BarLegend[{"GreenPinkTones", {-10, 10}}];

dp1 = DensityPlot[Sin[x] + y, {x, -4, 4}, {y, -3, 3}, 
   ColorFunctionScaling -> False, 
   ColorFunction -> ColorData[{"GreenPinkTones", {-10, 10}}], 
   ImageSize -> Medium];

dp2 = DensityPlot[Sin[x] + 3 y, {x, -4, 4}, {y, -3, 3}, 
   ColorFunctionScaling -> False, 
   ColorFunction -> ColorData[{"GreenPinkTones", {-10, 10}}], 
   ImageSize -> Medium];

Legended[Row[{dp1, dp2}, Spacer[2]], bl]

which gives me a common colour scale for both the plots with a common legend as below:

colorbar1

Now if I tweak my code using the ColorBar package, like this:

First, I load the package and define a newly tweaked colour theme:

Needs["ColorBar`"]
    
newtheme = Setting@ColorBar["GreenPinkTones"];

where I use "Evaluate in place" or CtrlShiftEnter after selecting ColorBar["GreenPinkTones"] on my Windows machine, which gives me the option to tweak some colours, like the one I provided here:

newtheme

Now, I proceed as below:

bl = BarLegend[{newtheme, {-10, 10}}];

dp1 = DensityPlot[Sin[x] + y, {x, -4, 4}, {y, -3, 3}, 
   ColorFunctionScaling -> False, 
   ColorFunction -> {newtheme, {-10, 10}}, ImageSize -> Medium];

dp2 = DensityPlot[Sin[x] + 3 y, {x, -4, 4}, {y, -3, 3}, 
   ColorFunctionScaling -> False, 
   ColorFunction -> {newtheme, {-10, 10}}, ImageSize -> Medium];

Legended[Row[{dp1, dp2}, Spacer[2]], bl]

which gives me some warning messages and the following output:

colorbar2

Where am I making a mistake? What changes should I make in my code to make it work?

Any help in this regard would be beneficial!

$\endgroup$
2
  • 1
    $\begingroup$ You're not making a mistake. I think the right place to add this flexibility would be in the ColorBar package. 0-1 range is hardcoded in, whereas it should accept a min/max parameter and use ColorData[{gradient, {min, max}}] to do the rescaling (and replace all other 0/1 with min/max). As a short-term solution, I probably would've added /.{x__?NumericQ}:>Rescale[{x},{0,1},{-10,10}] on the line defining newtheme just so that I don't have to worry about the number of control points. But until the package is updated, either this or your solution is fine. $\endgroup$
    – rm -rf
    Feb 8 at 15:12
  • $\begingroup$ @rm-rf thanks for providing a short-term solution. I hope you can update the package soon. It's really helpful indeed! $\endgroup$
    – codebpr
    Feb 8 at 15:22

1 Answer 1

1
$\begingroup$

I found a manual way to do this, but it's quite tedious:

  1. Make a newly tweaked colour scheme:

newcolortheme

  1. Change the range of your chosen theme according to your needs:
newtheme[[1, 1, 1, 1]] = Range[-10, 10, 20/15];
  1. Now, make some adjustments to the previous code:
bl = BarLegend[{newtheme[#] &, {-10, 10}}];

dp1 = DensityPlot[Sin[x] + y, {x, -4, 4}, {y, -3, 3}, 
   ColorFunctionScaling -> False, ColorFunction -> newtheme, 
   ImageSize -> Medium];

dp2 = DensityPlot[Sin[x] + 3 y, {x, -4, 4}, {y, -3, 3}, 
   ColorFunctionScaling -> False, ColorFunction -> newtheme, 
   ImageSize -> Medium];

Legended[Row[{dp1, dp2}, Spacer[2]], bl]

which gives the desired result:

colorbar3

I hope others can provide better solutions to make this process automated and more efficient!

Edit: Thanks to @rm -rf, the developer of the package for providing a better solution in the comments:

newtheme = 
 Setting@ColorBar["GreenPinkTones"] /. {x__?NumericQ} :> 
   Rescale[{x}, {0, 1}, {-10, 10}]
$\endgroup$

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.