# Increase the contrast in Z scale

I use the following code to plot my 2D map. I would like to increase the contrast in Z scale in order to see better the details in the blue area (-0.5 to 0) and red area (0.5 to 1) better and put in white all the data between 0 and 0.5. I don't see how to do it (I tried RegionFunction). In attached the picture I get and the data can be found here or here.

SetDirectory[NotebookDirectory[]];
file = FileNames["*.dat", NotebookDirectory[]];
func[file_String /; FileExistsQ[file]] :=
Module[{data, dataT}, data = Import[file, "Table", HeaderLines -> 2];
dataT = Transpose[data];
dataT = {dataT[]*10, dataT[]*10,
dataT[]}; (*je converti de angtroms a nano*)

dataT[] = Rescale[dataT[], MinMax[dataT[]], {-1., 1.}];
data = Transpose[dataT];

graph = ListDensityPlot[data,
PlotLabel -> FileBaseName[file],
PlotRange -> {{-0.5, 0.5}, {-0.5, 0.5}, {-1., 1.}},
ColorFunction -> (ColorData[{"TemperatureMap", {-0.8, 0.8}}])]

]

func /@ file • Does this match your color range? myTemperatureMap[f_] := Blend[{{0., Blue}, {.25, Lighter@LightBlue}, {.25, White}, {.75,White}, {.75, Yellow}, {1., Red}}, f] ... I tried it, but the results are exaggerated. – creidhne Jun 27 '18 at 9:37

Use Blend to define a color gradient.

myTemperatureMap[f_] :=
Blend[{{0., Blue}, {.25, Lighter@LightBlue}, {.25, White}, {.75,
White}, {.75, Yellow}, {1., Red}}, f]


Compare myTemperatureMap to the named color gradient, "TemperatureMap".

LinearGradientImage[myTemperatureMap, {200, 20}, DataRange -> {0, 1}] LinearGradientImage["TemperatureMap", {200, 20}, DataRange -> {0, 1}] Here are myTemperatureMap and "TemperatureMap" applied to a density plot:

DensityPlot[Sin[x] Sin[y], {x, -3, 3}, {y, -3, 3}, PlotPoints -> 100,
ColorFunction -> myTemperatureMap, PlotLegends -> Automatic] DensityPlot[Sin[x] Sin[y], {x, -3, 3}, {y, -3, 3}, PlotPoints -> 100,
ColorFunction -> "TemperatureMap", PlotLegends -> Automatic] • Hi, Thanks a lot, I get to the same conclusion. Your way to include Blend in a function myTemperatureMap is more nice compare to what I wrote (not shown) – Bigprophete Jun 27 '18 at 11:47
cf1 = If[Abs[#] <= .5, White, ColorData[{"TemperatureMap", {-1, 1}}][#]] &;

cf2 = Blend[Join[{#, ColorData[{"TemperatureMap", {-1, 0}}][#]} & /@ Range[-1, -.5, .1],
{{-.5, White}, {.5, White}},
{#, ColorData[{"TemperatureMap", {0, 1}}][#]} & /@ Range[.5, 1., .1]], #] &;


Using the example in creidhne's answer:

dp = DensityPlot[Sin[x] Sin[y], {x, -3, 3}, {y, -3, 3},
PlotLegends -> Automatic, ImageSize -> 300, PlotPoints -> 200,
MaxRecursion -> 5, MeshFunctions -> {#3 &}, Mesh -> {{-.5, .5}},
ColorFunction -> "TemperatureMap"];

{dpcf1, dpcf2} = DensityPlot[Sin[x] Sin[y], {x, -3, 3}, {y, -3, 3},
PlotPoints -> 200,  MaxRecursion -> 5, ImageSize -> 400, PlotLegends -> Automatic,
MeshFunctions -> {#3 &}, Mesh -> {{-.5, .5}},
ColorFunction -> #, ColorFunctionScaling -> False] & /@ {cf1, cf2};

Row[{dp, dpcf1, dpcf2}] You can get the same result for the main plot using cf1 or using RegionFunction but the legend is not affected by the RegionFunction setting:

DensityPlot[Sin[x] Sin[y], {x, -3, 3}, {y, -3, 3},
PlotLegends -> Automatic, ImageSize -> 300, PlotPoints -> 200,
ColorFunction -> "TemperatureMap",
RegionFunction -> (Abs[#3] >= .5 &),
BoundaryStyle -> Directive[Thin, Gray]] 