How can I easily control the color shading of ArrayPlot? Say I want to go from white to dark green in a gradient form.
2 Answers
$\begingroup$
$\endgroup$
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]]]
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]}]
$\begingroup$
$\endgroup$
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"}]
Use (Blend[{White, Green}, #] &)
to get
ListDensityPlot[matrix, ColorFunction -> (Blend[{Green, White}, #] &), ScalingFunctions -> {None, "Reverse"}]
? (usingmatrix
from your other question) $\endgroup$