4
$\begingroup$

I would like to make a density plot of a function: $f=B(r^2+z^2)sin^2z$ for some range of parameter B. The color scale for each graph has a different value. For instance, for $B=5: f=600$ is white color, $f=0$ is black color, but for $B=25: f=3000$ is white color, $f=0$ is black color.

How could it be made so that the color scales for all graphs (for each $B$) show the same range of change in the function? For example, the same scale as for the maximum parameter $B$ $(Bmax = 60)$: for B=0 scale range 0-3000, for B=5 scale range 0-3000, for B=10 scale range 0-3000, for B=25 scale range 0-3000.

ClearAll["Global`*"]

f[B_] := B*(r^2 + z^2)*Sin[z]^2;

Table[DensityPlot[f[B], {r, 0, 10}, {z, -5, 5}, FrameLabel -> {r, z}, 
  PlotLabel -> "B = " <> ToString[B], ColorFunction -> "SunsetColors",
   PlotPoints -> 250, PlotLegends -> Automatic], {B, {0, 5, 10, 25}}]
$\endgroup$

2 Answers 2

6
$\begingroup$

Perhaps something like this? You just need to define the ColorFunction and set ColorFunctionScaling to False. I also adjusted the bar legends (they're all the same now since they're all on the same color scaling).

f[B_] := B*(r^2 + z^2)*Sin[z]^2;

Table[DensityPlot[f[B], {r, 0, 10}, {z, -5, 5}, FrameLabel -> {r, z}, 
  PlotLabel -> "B = " <> ToString[B], 
  ColorFunction -> (ColorData["SunsetColors"][#/3000] &), 
  PlotPoints -> 250, 
  PlotLegends -> 
   BarLegend[{ColorData["SunsetColors"][#/3000] &, {0, 3000}}], 
  ColorFunctionScaling -> False], {B, {0, 5, 10, 25}}]

enter image description here

$\endgroup$
4
$\begingroup$
$Version

(* "13.3.1 for Mac OS X ARM (64-bit) (July 24, 2023)" *)

Clear["Global`*"]

f[r_, B_, z_] := B*(r^2 + z^2)*Sin[z]^2;

Determine the range of the function within the region of interest

{min, max} = #[{f[r, B, z], 0 <= r <= 10, -5 <= z <= 5, 0 <= B <= 25}, 
  {r, B, z}] & /@ {NMinValue, NMaxValue}

(* {0., 3059.73} *)

For a common scale but displaying only tailored portions of the scale,

Partition[
  Table[
   DensityPlot[f[r, B, z], {r, 0, 10}, {z, -5, 5},
    FrameLabel -> {r, z}, PlotLabel -> StringForm["B = ``", B],
    ColorFunction -> (ColorData["SunsetColors"][(# - min)/(max - min)] &),
    ColorFunctionScaling -> False,
    PlotPoints -> 250,
    PlotLegends -> Automatic,
    ImageSize -> 300],
   {B, {0, 5, 10, 25}}], 2] // Grid

enter image description here

For identical scales,

Partition[
  Table[
   Legended[
    DensityPlot[f[r, B, z], {r, 0, 10}, {z, -5, 5},
     FrameLabel -> {r, z}, PlotLabel -> StringForm["B = ``", B],
     ColorFunction -> (ColorData["SunsetColors"][(# - min)/(max - min)] &),
     ColorFunctionScaling -> False,
     PlotPoints -> 250,
     ImageSize -> 300],
    BarLegend[{"SunsetColors", {min, max}}]],
   {B, {0, 5, 10, 25}}], 2] // Grid

enter image description here

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