4
$\begingroup$

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

risk matrix manipulation

Desired output would be something like this

risk matrix desired output

$\endgroup$
2
  • 1
    $\begingroup$ I would suggest combining ContourPlot with underlying ArrayPlot with the help of Show. $\endgroup$
    – kirma
    Commented Jun 21, 2020 at 11:50
  • $\begingroup$ I am currently experimenting with ColorFunction $\endgroup$ Commented Jun 21, 2020 at 11:51

1 Answer 1

7
$\begingroup$

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]

enter image description here

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 /@ 
     Thread[{PlotRange[lcp], Dimensions[mat]}]);

heightspecs = Reverse @ mat &;

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

Show[dh, lcp]

enter image description here

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]

enter image description here

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]]}]]

enter image description here

$\endgroup$
5
  • 1
    $\begingroup$ 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! $\endgroup$ Commented Jun 21, 2020 at 15:36
  • 2
    $\begingroup$ @e.doroskevic, glad it was useful. Thank you for the accept. $\endgroup$
    – kglr
    Commented Jun 21, 2020 at 16:22
  • $\begingroup$ 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? $\endgroup$ Commented Jun 21, 2020 at 16:34
  • $\begingroup$ @kglr A very good example in MMA! $\endgroup$
    – ABCDEMMM
    Commented Jun 21, 2020 at 17:36
  • $\begingroup$ @kglr I just got back to this question, and I noticed the edit - wow! $\endgroup$ Commented Jun 26, 2020 at 20:52

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.