How to assign special colors to the output of DensityPlot?

I have created the following function for plotting

plotDynamical[iterMethod_, points_] :=
DensityPlot[
iterAlgorithm[iterMethod], {t, xxMin, xxMax}, {s, yyMin,yyMax},
PlotRange -> {1,4}, ColorFunction -> {Orange, Blue, Black, Green},
PlotPoints -> points]


The possible results of " iterAlgorithm[iterMethod] " are 1 , 2, 3 or 4. I would like to assigning colours to numbers like so: Orange to 1,Blue to 2,Black to 3 and Green to 4. How can I do this?

complete my Algorithm is:

F = Compile[{{t, _Real}, {s, _Real}}, {t^2 + s^2 - 4, -Exp[t] + s -
1}];
dF = Compile[{{t, _Real}, {s, _Real}}, {{2 t, 2 s}, {-E^t, 1}}];
invdF = Compile[{{t, _Real}, {s, _Real}}, {{1/(
2 E^t s + 2 t), -((2 s)/(2 E^t s + 2 t))}, {E^t/(
2 E^t s + 2 t), (2 t)/(2 E^t s + 2 t)}}];

rootF[1] = {-1.59832066552612835, 1.202235854627582} ;
rootF[2] = {0, 2} ;

rootPosition =
Compile[{{t, _Real}, {s, _Real}},
Which[Norm[{t, s} - rootF[1]] < 10.0^(-10), 3,
Norm[{t, s} - rootF[2]] < 10.0^(-10), 2, True,
1], {{rootF[_, _], _Real, _Real}}];

iterPsM10 = Compile[{{t, _Real}, {s, _Real}},
Block[{v = F[t, s], w = dF[t, s], u = invdF[t, s], x, y, z, dFz, Q,
uu, vv, Fu, vu, invdFvu},
x = {t, s};
y = x - (1/2 ) u.v;
z = 1/3 (4 y - x);
dFz = dF @@ ({z[[1]], z[[2]]});
Q = Inverse[w - 3 dFz];
uu = y + Q.v;
Fu = F @@ ({uu[[1]], uu[[2]]});
vv = uu + 2 Q.Fu;
vu = 1/2 (vv + uu);
invdFvu = invdF @@ ({vu[[1]], vu[[2]]});
uu - invdFvu.Fu]];

iterAlgorithm[iterMethod_, lim_] :=
Block[{ct, r}, ct = 0; r = rootPosition[t, s];
While[(r == 1) && (ct < lim), ++ct; {t, s} = iterMethod[t, s];
r = rootPosition[t, s]];
If[Head[r] == Which, r = 0];(*"Which" unevaluated*)Return[r]];

limIterations = 1000;
xxMin = -5; xxMax = 5; yyMin = -5; yyMax = 5;

plotDynamical[iterMethod_, points_] :=
DensityPlot[iterAlgorithm[iterMethod, limIterations],
{t, xxMin, xxMax}, {s, yyMin, yyMax}, PlotRange -> {0, 3},
ColorFunction -> {Green, Black, Orange, Blue},
PlotPoints -> points,
Epilog -> {White, PointSize[.02], Point[rootF[1]], Point[rootF[2]]}];

plotDynamical[iterPsM10, 56]

-
Hi @user14345, welcome to Mathematica.SE. I have edited your question best as I could but it's still unclear as it's depending on the output of the function iterAlgorithm. Could you perhaps post a minimal example of how it works? Also, it will be helpful to read the help section (you get a badge for doing so!) and in particular the markdown guide on how to better format your questions. Don't forget to upvote other people's questions and answers if you think they are worth it! – gpap May 15 '14 at 8:59
Also it's a good idea to post stand-alone code (code that someone will copy-paste and it will run as is). The way your question is formed, one needs not only the funciton iterAlgorithm but also xxMin, xxMax, yyMin, yyMax. Finally, perhaps you may want to switch your username to something a little more memorable? – gpap May 15 '14 at 9:00

2 Answers

As you don't provide a working example I have used a simple function to plot a DensityPlot. You can use for example a Piecewise function as the ColorFunction to determine the colour of your data points:

DensityPlot[Sin[x y], {x, 0, 3}, {y, 0, 3},
ColorFunction ->
Function[{x, y},
Piecewise[{{Orange, 0 <= x < 0.25}, {Blue, 0.25 >= x < 0.5}, {Red,
0.5 <= x < 0.75}, {Green, 0.75 <= x <= 1}}, Yellow]],
PlotPoints -> 50]


In your case you would have to set the ranges to Piecewise[{{Orange, x==1},{Blue, x==2},{ and so on

-
how to use this method in above algorithm? – user14345 May 15 '14 at 10:28
This idea can't work with this algorithm . – user14345 May 15 '14 at 12:41
The above method dosn't work correctly!the output colors is't correct. – user14345 May 15 '14 at 22:52
spot following example: – user14345 May 15 '14 at 22:54
f[x_, y_] := Which[x/y == 2, 2, x/y == 3, 3, x/y == 1, 0, True, 1]; DensityPlot[f[x, y], {x, 0, 3}, {y, 0, 3}, ColorFunction -> Function[{x, y}, Piecewise[{{Blue, x == 3}, {Orange, x == 2}, {Black, x == 1}, {Green, x == 0}}, Yellow]], PlotPoints -> 32] – user14345 May 15 '14 at 22:54

(Minor brain failure, but after applying the jumper cables...)

Here is a function that returns a value in the set (1, 2, 3, 4):

fn = Floor @ Mod[# + Sin[#2], 4, 1] &


In a DensityPlot:

DensityPlot[fn[t, s], {t, 0, 10}, {s, 5, 15}, PlotPoints -> 50,
ColorFunction -> {Orange, Blue, Black, Green}]


Or as a ContourPlot:

ContourPlot[fn[t, s], {t, 0, 10}, {s, 5, 15},
Contours -> {1, 2, 3, 4},
ContourShading -> {Green, Orange, Blue, Black},  (* thanks kguler *)
PlotPoints -> 50
]


-
ContourShading -> {Orange, Blue, Black, Green} instead of ColorFunction->... also works (+1) – kglr May 15 '14 at 23:09
@kguler ah, yes... – Mr.Wizard May 15 '14 at 23:09
@kguler I think the order has to be changed though; see above. Do you agree? – Mr.Wizard May 15 '14 at 23:20
yes... need to rotate right the color list - not sure why though. – kglr May 15 '14 at 23:41