0
$\begingroup$

I have 5 ListDensityPlots that are using the same color function but the color bar runs over a different range in every plot. For example here are two of the 5 plots that I have

enter image description here enter image description here

I want these plots to have the same coloring as the following two plots

enter image description here enter image description here

I know that I can rescale these plots using the Rescale function but am unsure of how to change the colors so that the colors show up in the same manner as the last two figures. Currently my code without using the Rescale function looks like the following for plotting:

rainbow[z_] := Blend[{Black, Purple, Blue, Green, Yellow, Red}, z]

figure = ListDensityPlot[INSplot, PlotRange -> All, ColorFunction -> rainbow, LabelStyle -> {18, GrayLevel[0], Italic, 
FontFamily -> "Helvetica"}, FrameLabel -> {"[H,0,0] (r.l.u)", "[-H,2H,0] (r.l.u)", "3T, E = [0.6, 0.7] meV"}, 
PlotLegends -> Placed[BarLegend[Automatic, 
 LegendLabel -> 
  Placed[Rotate["Intensity (arb.unit.)", 90 Degree], Right
   ], 
 LabelStyle -> {18, GrayLevel[0], Italic, 
   FontFamily -> "Helvetica"}], Right]]
$\endgroup$

1 Answer 1

3
$\begingroup$

If I understand the question correctly, you need to do two things - turn off ColorFunctionScaling, and then rescale your input before you feed it to your color function. You will need to do the same thing to your legend to get it to display the colors correctly in the range.

rainbow[z_] := Blend[{Black, Purple, Blue, Green, Yellow, Red}, z];
d1 = Table[
   Sin[x] + Cos[y], {x, 0, 2 \[Pi], 2 \[Pi]/100}, {y, 0, 2 \[Pi], 
    2 \[Pi]/100}];
d2 = 10*d1;
mx = Max[Flatten@{d1, d2}]; (*For finding the new scale.*)
mn = Min[Flatten@{d1, d2}]; (*For finding the new scale.*)
ListDensityPlot[
 d1
 , ColorFunctionScaling -> False
 , ColorFunction -> (rainbow[Rescale[#, {0, mx}, {0, 1}]] &)
 ]

ListDensityPlot[
 d2
 , ColorFunctionScaling -> False
 , ColorFunction -> (rainbow[Rescale[#, {0, mx}, {0, 1}]] &)
 ]
legend = BarLegend[{rainbow[Rescale[#, {mn, mx}, {0, 1}]] &, {mn, mx}}]

enter image description here enter image description here enter image description here

Rescale will rescale your data so that numbers in the range {mn,mx} are now mapped from {0,1}, which is where most color functions reside. This normally happens automatically, rescaling the data to get good coverage from the color function, which is why you have to turn off ColorFunctionScaling.

If a number is larger than mx, or smaller than mn it will be mapped to the appropriate number greater or smaller than 1 and it will clip on your color scale.

$\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.