4
$\begingroup$

I would like to plot a figure using colorfunction in Mathematica. I opened the "Color Schemes" from the Palettes menu, but did not find a proper colorfunction.

Questions: Please see the below image.

  1. Can I use only a part of "Rainbow" colorfunction? I do not want to use the dark blue side. For this case, how to rescale the new color range to be 0-1?
  2. Furthermore, it looks the red side of "Rainbow" is not dark enough. Can I further add a red part of "Visible Spectrum" on it? and then rescale the new color range to be 0-1 again.

May someone help me? Thank you in advance.

enter image description here

$\endgroup$

1 Answer 1

7
$\begingroup$

We start by defining a function for visualizing color functions:

colorbar[cf_] := DensityPlot[
  x,
  {x, 0, 370},
  {y, 0, 50},
  ColorFunction -> cf,
  ColorFunctionScaling -> True,
  AspectRatio -> Automatic,
  PlotRangePadding -> 10,
  FrameTicks -> {
    {None, None},
    {
     Transpose[{
       Subdivide[370, 5],
       N@Subdivide[5]
       }],
     None}
    }]

The problems that you mention can be solved using Rescale.

Using only a part of the rainbow color scheme:

cf = ColorData["Rainbow", Rescale[#, {0, 1}, {0.25, 1}]] &;
colorbar[cf]

Output

Using only a part of the visible spectrum color function:

cf = ColorData["VisibleSpectrum", Rescale[#, {0, 1}, {625, 750}]] &;
colorbar[cf]

Output

Combining parts from different color functions:

cf = Which[
    # < 0.8,
    ColorData["Rainbow", Rescale[#, {0, 0.8}, {0.25, 1}]],
    # >= 0.8,
    ColorData["VisibleSpectrum", Rescale[#, {0.8, 1}, {625, 750}]]
    ] &;
colorbar[cf]

Output

Another way is to sample colors from the color functions and put them together using Blend:

cf = Blend[Join[
     Table[
      {0.8 (x - 0.25)/(1 - 0.25), ColorData["Rainbow", x]},
      {x, 0.25, 1, 0.01}],
     Table[
      {0.8 + 0.2 (x - 625)/(750 - 625), ColorData["VisibleSpectrum", x]},
      {x, 625, 750}
      ]
     ], #] &;
colorbar[cf]

Output

$\endgroup$
1
  • 1
    $\begingroup$ Hi, C. E.,thank you. This is what I expected. :-) $\endgroup$
    – Mr.2023
    Commented Jul 10, 2020 at 7:59

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.