# How-to control color range assignment in ListContourPlot

I haven't been using Mathematica for some time. So, I got a bit stuck, and I would appreciate some help :).

I would like to...

• Assign specific colors to the grid cells
• Generate a legend, based on the above
• Overlay a set of contours where value of f is lower than a set of pre-defined values e.g. {.25, .5, .75}

Following is the code I am working with just now

ListContourPlot[
Outer[{#1, #2*10^2, #1*#2*10^2} &, #, #] & @ Range[0, 1, .02] //
Flatten[#, 1] &
, Mesh -> 4
, MeshFunctions -> {#1 &, #2 &}
, Contours -> 3
, ContourStyle -> Black
, ContourLabels -> True
, PlotLegends -> Automatic
, ContourShading -> {Green, Yellow, Orange, Red}
]

Output of the above code can be seen below

Desired output would be something like this

• I would suggest combining ContourPlot with underlying ArrayPlot with the help of Show. Jun 21, 2020 at 11:50
• I am currently experimenting with ColorFunction Jun 21, 2020 at 11:51

maybe something like:

data1 = Outer[{#1, #2*10^2, #1*#2*10^2} &, #, #] &@Range[0, 1, .02] //
Flatten[#, 1] &;

data2 = Outer[{#1, #2*10^2, Ceiling[#1/.2]*Ceiling[#2 10^2/20]} &, #, #] &@
Range[0, 1, .02] // Flatten[#, 1] &;

intF = Interpolation[data2, InterpolationOrder -> 0];

cp = ContourPlot[intF[x, y], {x, 0, 1}, {y, 0, 100}, Contours -> 3,
PlotPoints -> 60, ContourShading -> {Green, Yellow, Orange, Red}];

Show[cp, lcp]

### DensityHistogram

Alternatively, use DensityHistogram with an input array of color codes:

mat = {{2, 3, 3, 4, 4}, {2, 2, 3, 3, 4}, {1, 2, 2, 3, 3}, {1, 1, 2, 2, 3}};

colorFunc = Blend[{{1, Green}, {2, Yellow}, {3, Orange}, {4, Red}}, #1] &;

binspecs = {Subdivide[##]} & @@@ (Flatten /@

heightspecs = Reverse @ mat &;

dh = DensityHistogram[{{1, 1}}, binspecs, heightspecs,
ColorFunction -> colorFunc, ColorFunctionScaling -> False, Mesh -> All];

Show[dh, lcp]

### ListDensityPlot

You can also pad mat and use it with ListDensityPlot

paddedmat = ArrayPad[Reverse /@ Transpose@mat, {{0, 1}, {0, 1}}, "Fixed"];

ldp = ListDensityPlot[paddedmat, Mesh -> All,
InterpolationOrder -> 0, DataRange -> {{0, 1}, {0, 100}},
ColorFunction -> colorFunc, ColorFunctionScaling -> False];

Show[ldp, lcp]

### EventHandler + DensityHistogram

Specify the bin colors interactively using mouse clicks:

dims = {4, 5};
DynamicModule[{m = ConstantArray[1, dims],
nc = Length[colorFunc[[1, 1]]],
binspecs = {Subdivide[##]} & @@@ (Flatten /@ Thread[{PlotRange[lcp], dims}]),
sc = PlotRange[lcp][[All, 2]]/dims},
EventHandler[Dynamic@Show[
DensityHistogram[{{1, 1}}, binspecs, Reverse @ m &,
ImageSize -> Large, AspectRatio -> 1, ColorFunction -> colorFunc,
ColorFunctionScaling -> False, Mesh -> All, MeshStyle -> Gray,
ImageSize -> Large, PerformanceGoal -> "Speed"],
lcp],
{{"MouseClicked", 1} :> With[{p = {1 + dims[[1]] - #, #2} & @@
Ceiling[(MousePosition["Graphics"])/sc]}, m[[## & @@ p]] =
Mod[m[[## & @@ p]] + 1, nc, 1]],
{"MouseClicked", 2} :> With[{p = {1 + dims[[1]] - #, #2} & @@
Ceiling[(MousePosition["Graphics"])/sc]},
m[[## & @@ p]] = Mod[m[[## & @@ p]] - 1, nc, 1]]}]]

• I like this solution, and I am happy to accept it as an answer. I believe, I can use this to continue my work. I do have more issues with this little project of mine but they merit their own question, here at stackexchange :D Thank you! Jun 21, 2020 at 15:36
• @e.doroskevic, glad it was useful. Thank you for the accept.
– kglr
Jun 21, 2020 at 16:22
• maybe you know, would there be an easy way how I could apply a rule to color the grid? For instance, f[x] < .25 -> Green, f[x] < .5 -> Yellow? Jun 21, 2020 at 16:34
• @kglr A very good example in MMA! Jun 21, 2020 at 17:36
• @kglr I just got back to this question, and I noticed the edit - wow! Jun 26, 2020 at 20:52