3
$\begingroup$

This question already has an answer here:

I have several functions of two variables defined in some region, say

functions[x_,y_]:={0,x-y,Sin[x^2-y]}

I'd like a plot to show at a glance the regions where each function is largest, with a different colour or similar. One approach is to just do a RegionPlot for every function, showing where it's biggest, and stick them all together, something like this (apologies for the hideous colours):

Show@Array[RegionPlot[{#} == Ordering[functions[x, y], -1], {x,0,1}, {y,0,1},
      MaxRecursion -> 10, PlotStyle -> ColorData[1, "ColorList"][[#]]] &, 3]

Output

This is fine as far as it goes, but I can't help but feel there's a better way. For example, if my functions are more expensive to evaluate, and if there are many of them, this is slow, since everything must be done many times.

For two functions, ContourPlot on the difference seems exceedingly sensible, but I can't think of how to generalize it to more than two.

So: what other clever ways might you suggest to implement this?

$\endgroup$

marked as duplicate by Rahul, Kuba, Öskå, gpap, m_goldberg Nov 20 '14 at 15:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1
$\begingroup$
functions[x_, y_] := {.3, x Sin@y, y Sin@x}
f[x_, y_] := First@Ordering[functions[x, y] // N, -1]
ContourPlot[f[x, y], {x, 0, 1}, {y, 0, 1}, PlotPoints -> 30]

Mathematica graphics

$\endgroup$
1
$\begingroup$

Modifying several alternatives suggested in a closely related Q/A: Plot the plane so that ...

flist = {.3, x Sin@y, y Sin@x};

Plot3D viewed from above:

p3d = Plot3D[flist, {x, 0, 1}, {y, 0, 1},
   PlotStyle -> ColorData[1, "ColorList"][[;; 3]],
   Mesh -> None, ViewPoint -> {0, 0, ∞}, Boxed -> False, 
   Axes -> False, Lighting -> "Neutral", PlotRangePadding -> 0, 
   ImagePadding -> 25, ImageMargins -> 0];
frame2d = Plot[x, {x, 0, 1}, Frame -> True, PlotStyle -> None,
   AspectRatio -> 1, PlotRangePadding -> 0, ImagePadding -> 20,
   ImageMargins -> 0, Axes -> False];
Overlay[{p3d, frame2d}, Alignment -> Center]

enter image description here

ContourPlot:

pieceW = Piecewise[Table[{1. i, flist[[i]] == Max[flist]}, {i, 1, Length@flist}]];
ContourPlot[pieceW, {x, 0, 1}, {y, 0, 1}, PlotPoints -> 100, 
 ImageSize -> 400, Contours -> Range[3], 
 ContourShading -> RotateRight[ColorData[1, "ColorList"][[;; 3]]],
 ContourStyle -> ColorData[1, "ColorList"][[;; 3]], Exclusions -> None]

enter image description here

DensityPlot:

DensityPlot[pieceW, {x, 0, 1}, {y, 0, 1}, PlotPoints -> 100, 
 ImageSize -> 400, ColorFunction -> (ColorData[1, "ColorList"][[#]] &), 
 Exclusions -> None, ColorFunctionScaling -> False, ImagePadding -> 25]

enter image description here

RegionPlot:

regions = And @@@ (Outer[GreaterEqual, flist, flist] /.  x_ >= x_ :> Sequence[]);
RegionPlot[regions, {x, 0, 1}, {y, 0, 1}, ImageSize -> 400, 
 PlotPoints -> 100,  PlotStyle -> ColorData[1, "ColorList"], ImagePadding -> 25]

enter image description here

$\endgroup$
  • $\begingroup$ Is this question "closely related" or an actual duplicate of the one you linked to? It certainly looks like a duplicate to me, but I haven't voted to close yet in case I'm missing something. $\endgroup$ – Rahul Nov 20 '14 at 9:05
  • $\begingroup$ Are the questions different? "I have several functions of two variables defined in some region ... I'd like a plot to show at a glance the regions where each function is largest, with a different colour or similar" perfectly describes the other question (except for "smallest" vs. "largest"). $\endgroup$ – Rahul Nov 20 '14 at 9:20
  • $\begingroup$ I do kind of want to hear that aside, but I'll understand if you're not inclined to typing it all up! $\endgroup$ – Rahul Nov 20 '14 at 9:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.