# How to set the same scaling for multiple density plots while using the ColorBar package or user-defined colorfunction?

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:

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:

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:

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!

• 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.
– rm -rf
Feb 8 at 15:12
• @rm-rf thanks for providing a short-term solution. I hope you can update the package soon. It's really helpful indeed! Feb 8 at 15:22

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

1. Make a newly tweaked colour scheme:

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:

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