# ListDensityPlot with discrete bins [duplicate]

We consider a table of the form

data = Flatten[Table[{i,j,f[i,j]},{i,1,ni},{j,1,nj}],1]


The function f[i_,j_] has been made so that 0 ≤ f[i,j] ≤ 1. I can plot this map using

ListDensityPlot[data, ColorFunction -> "Rainbow", InterpolationOrder -> 0]


However, the colors attributed to the values of f[i,j] are continuous and vary smoothly. I would like to have only a finite number of colors. For example : 0 ≤ f[i,j] ≤ 1/2 --> Red and 1/2 ≤ f[i,j] ≤ 1 --> Blue (Of course, my real bins will be different, but this is the idea.)

How could I proceed to make this plot so that only a finite number of colors are present on the plot with custom ranges ? I guess it has something to do with the ColorFunction, but I can't get the syntax correct...

• @Kuba I took out the definitions in order to get a minimum working example as simple as possible. Your suggestion definitely works using ColorFunction -> (If[# < .5, Red, Blue] &), ColorFunctionScaling -> False. However, I still don't understand this syntax. I would prefer to define a function fcolor[value_] which would give me the color associated to a value f[i_,j_]. What would then be the syntax for ColorFunction -> ... ?
– jibe
May 13 '14 at 8:15
• Simple: ColorFunction -> fcolor May 13 '14 at 9:30

## 1 Answer

Take a look at ColorFunction, ColorFunctionScaling, and InterpolationOrder, e.g.:

ListDensityPlot[{{0, 0, 0}, {1, 0, 1}, {0, 1, 2}, {1, 1, 3}},
ColorFunctionScaling -> None, InterpolationOrder -> 0,
ColorFunction ->
Function[arg,
Which[0 <= arg < 1, Red, 1 <= arg < 2, Blue, 2 <= arg < 3, Green, True, Orange]]]


You can use similar constructs with MatrixPlot and ArrayPlot, which may be better fits for your needs.

Per your comment, if you want to externalize the color function, e.g.:

fcolor[arg_] :=
Which[0 <= arg < 1, Red, 1 <= arg < 2, Blue, 2 <= arg < 3, Green, True, Orange]

ListDensityPlot[{{0, 0, 0}, {1, 0, 1}, {0, 1, 2}, {1, 1, 3}},
ColorFunctionScaling -> None, InterpolationOrder -> 0,
ColorFunction -> fcolor]

• Thank you. As I commented above to Kuba. Would you which syntax I could use to define the function Function[arg, Which[0 <= arg < 1, Red, 1 <= arg < 2, Blue, 2 <= arg < 3, Green, True, Orange]] out of the ListDensityPlot ? With such a function fcolor[value_], what would become the part ColorFunction -> .. ? (This is an additional question, your approach works perfectly.)
– jibe
May 13 '14 at 8:18
• @jibe: see edit. Read docs re: details on the functionality of the various settings (I did the examples with what I think you had in mind). Also, use of Interval might be useful to you in defining ranges. Also, I'm a doofus - xzczd references the very question in which I show these methods - take a peek at it... and don't take it personally if this question is closed as a duplicate, helps keep things tight.
– ciao
May 13 '14 at 8:22
• This is exactly what I had in my mind. My next step will now be to get a nice PlotLegends on the side, but I will first try on my own. ^^
– jibe
May 13 '14 at 8:24
• Don't worry, I am not at all offended ! I am trying to use your answer to the duplicate, but the shorthened syntax using #, @ and & is not obvious for a beginner like me... In any case, thanks again.
– jibe
May 13 '14 at 8:32