4
$\begingroup$

How can I easily control the color shading of ArrayPlot? Say I want to go from white to dark green in a gradient form.

$\endgroup$
2
  • 2
    $\begingroup$ how about ListDensityPlot[matrix, ColorFunction -> (Blend[{Green, White}, #] &), ScalingFunctions -> {None, "Reverse"}]? (using matrix from your other question) $\endgroup$
    – kglr
    Commented Nov 29, 2020 at 1:44
  • $\begingroup$ Yes, worked! Thanksssss $\endgroup$ Commented Nov 29, 2020 at 2:01

2 Answers 2

7
$\begingroup$

Use Blend and ColorFunction to make a gradient of colors for ArrayPlot. Here's an example that uses a gradient of colors from white to dark green.

SeedRandom[123]
data = RandomReal[9, {10, 20}];
ArrayPlot[data, 
 ColorFunction -> Function[{a}, Blend[{White, Darker@Green}, a]]]

array plot with color function

Blend[{color1, color2}, x] works by mixing a fraction (1 - x) of the first color with a fraction x of the second color, where x is between 0 and 1. A table of 12 colors from white to green shows how Blend works.

Table[Blend[{White, Darker@Green}, x], {x, Subdivide[0, 1, 11]}]

blended colors

$\endgroup$
4
$\begingroup$
matrix = {{5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1}, 
 {5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1}, 
 {5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1}, 
 {5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1},
 {5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1}, 
 {5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1},
 {5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1},
 {5, 5, 5, 5, 5, 3, 3, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
 {5, 5, 5, 5, 3, 3, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
 {5, 5, 5, 3, 3, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
 {5, 5, 3, 3, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
 {5, 3, 3, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
 {3, 3, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
 {3, 3, 2, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
 {2, 2, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
 {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};

ListDensityPlot[matrix, 
  ColorFunction -> (Blend[{Green, White}, #] &),
  ScalingFunctions -> {None, "Reverse"}]

enter image description here

Use (Blend[{White, Green}, #] &) to get

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.