1
$\begingroup$

I have to sample and plot three functions: $f(x,y)=cos(\pi x /2)$, $g(x,y)=2-x^2$ and $r(x,y)=3x$ in the range x and y in [-1;1].

I want to make their density plots with a single ColorFunction to be applied to all: $0<=f(x,y)<=1$, $1<=g(x,y)<=2$, $-3<=r(x,y)<=3$.

All three density plots should be displayed with BarLegend scaled to [-1,1] with natural clipping. I have tried to do this in the following way:

F = Table[Cos[k*(Pi/2)], {j, -1.0, 1.0, 0.01}, {k, -1.0, 1.0, 0.01}];
G = Table[2 - k*k, {j, -1.0, 1.0, 0.01}, {k, -1.0, 1.0, 0.01}];
R = Table[3.0*k, {j, -1.0, 1.0, 0.01}, {k, -1.0, 1.0, 0.01}];

ListDensityPlot[F, ColorFunction -> "BlueGreenYellow", 
 PlotLegends -> Placed[BarLegend[{Automatic, {-1, 1}}], After], 
 FrameLabel -> {{"x", None}, {"y", None}}, ClippingStyle -> Automatic]

ListDensityPlot[G, ColorFunction -> "BlueGreenYellow", 
 PlotLegends -> Placed[BarLegend[{Automatic, {-1, 1}}], After], 
 FrameLabel -> {{"x", None}, {"y", None}}, ClippingStyle -> Automatic]

ListDensityPlot[R, ColorFunction -> "BlueGreenYellow", 
 PlotLegends -> Placed[BarLegend[{Automatic, {-1, 1}}], After], 
 FrameLabel -> {{"x", None}, {"y", None}}, ClippingStyle -> Automatic]

But this affects only legend bars and does not affect the color functions of plots. How can I reach the result I need?

$\endgroup$
1
  • $\begingroup$ Welcome to the Mathematica Stack Exchange. Please do not forget to accept one of the answers by clicking the checkmark next to the answer. If you find an answer useful, you can upvote it. This is the preferred way of saying Thanks on stack sites. You can take the site tour by clicking on the ribbon icons at the top-right of the page. $\endgroup$
    – Syed
    Feb 3, 2023 at 9:26

1 Answer 1

2
$\begingroup$
Clear["Global`*"]

F = Table[Cos[k*(Pi/2)], {j, -1., 1., 0.01}, {k, -1.0, 1.0, 0.01}];
G = Table[2 - k*k, {j, -1.0, 1.0, 0.01}, {k, -1.0, 1.0, 0.01}];
R = Table[3.0*k, {j, -1.0, 1.0, 0.01}, {k, -1.0, 1.0, 0.01}];

{min, max} = {-3, 3};

If they are all on the same scale, the common scale would be {-3, 3}. In this case, two of the plots would have reduced range.

Legended[Row[
  ListDensityPlot[ToExpression@#, 
     ColorFunction -> (ColorData["BlueGreenYellow"]
         [1 - (max - #)/(max - min)] &),
     ColorFunctionScaling -> False,
     FrameLabel -> {{"x", None}, {"y", None}},
     PlotLabel -> #,
     ImageSize -> Small] & /@
   {"F", "G", "R"}],
 Placed[
  BarLegend[{"BlueGreenYellow", {min, max}},
   LegendLayout -> "Row"],
  Below]]

enter image description here

If the intent is to clip the function values to {-1, 1}, then use the function Clip rather than the ClippingStyle option. However, G is then just 1

EDIT: Corrected ColorFunction and ColorFunctionScaling

Legended[
 Row[
  ListDensityPlot[
     Clip[ToExpression@#],
     ColorFunction -> 
      (ColorData["BlueGreenYellow"][(# + 1)/2] &),
     ColorFunctionScaling -> False,
     FrameLabel -> {{"x", None}, {"y", None}},
     PlotLabel -> #,
     ImageSize -> Small] & /@
   {"F", "G", "R"}],
 Placed[
  BarLegend[{"BlueGreenYellow", {-1, 1}},
   LegendLayout -> "Row"],
  Below]]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks! Its pretty close to what I needed. But I cann't understand why G is clipped at -1 instead of 1, and why F is -1 at 0 and 200 instead of being 0? $\endgroup$
    – Van
    Feb 2, 2023 at 17:05
  • $\begingroup$ Because I didn't pay attention and forgot to correct the ColorFunction and turn off ColorFunctionScaling $\endgroup$
    – Bob Hanlon
    Feb 2, 2023 at 18:26

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.