2
$\begingroup$

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[[1]]*10, dataT[[2]]*10, 
    dataT[[3]]}; (*je converti de angtroms a nano*)

  dataT[[3]] = Rescale[dataT[[3]], MinMax[dataT[[3]]], {-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

enter image description here

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – creidhne Jun 27 '18 at 9:37
2
$\begingroup$

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

myTemperatureMap

LinearGradientImage["TemperatureMap", {200, 20}, DataRange -> {0, 1}]

TemperatureMap

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]

density plot with myTemperatureMap

DensityPlot[Sin[x] Sin[y], {x, -3, 3}, {y, -3, 3}, PlotPoints -> 100, 
 ColorFunction -> "TemperatureMap", PlotLegends -> Automatic]

density plot with "TemperatureMap"

$\endgroup$
  • $\begingroup$ 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) $\endgroup$ – Bigprophete Jun 27 '18 at 11:47
1
$\begingroup$
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}]

enter image description here

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

enter image description here

$\endgroup$

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.