I have some data which I want to put in a DensityHistogram after i have clustered it. The different clusters should have different colors and based on how many are in one bin the color should have a different strength. I have no idea how to do this so any help would be appreciated.

data = RandomReal[{-1, 1}, {10^4, 2}];
n = 3;
clusters = FindClusters[data, n];
lengths = Length /@ clusters;
ranks = Ordering@Ordering[- lengths];
colors = {Red, Green, Blue}[[ranks]];
legends = SwatchLegend[colors, 
     "cluster " <> ToString[#] <> " : length = " <> ToString[lengths[[#]]] & /@ Range[n]];

  DensityHistogram[#, ColorFunction -> Function[{h}, Opacity[h, #2 ]]] & @@@ 
   Transpose[{clusters, colors}], PlotRange -> All], legends]

enter image description here

Use n=5 and colors = {Red, Green, Blue, Orange, Magenta}[[ranks]] to get:

enter image description here

Use the same bin width for each cluster ({.1}) to get

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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