I am trying to create a specific type of colour map for some data. The data is in the form of a list with dimensions 300000 x 3, that is 300,000 sets of {x, y, n} where x and y are basically the $xy$ cartesian coordinates and n is a value either 0 or 1. Doing a list plot of the 300,000 points gives the following figure:

enter image description here

What I am trying to make is a colour map of this figure, that is made up of 5 x 5 evenly sized squares (so 25 all up), in which the colour-key denotes the mean of the n values of all the points within that square. Is this possible?

Please let me know if any additional information is needed,


1 Answer 1

data = Join[RandomReal[1, {300000, 2}], RandomChoice[{0, 1}, {300000, 1}], 2];

{300000, 3}

nbins = 5;
binlims = Through[{Floor[Min@#, .01] &, Ceiling[Max@#, .01] &}@#] & /@ Transpose[data];
{xbins, ybins} = {##, -Subtract[##]/nbins} & @@@ Most[binlims];
binlists = BinLists[data, xbins, ybins, {0, 2, 2}];
binmeans =  Flatten /@ Map[Mean, binlists[[All, All, All, All, -1]], {-2}];

cft = Charting`FindTicks[{0, nbins}, {0, 1}];
MatrixPlot[binmeans, DataReversed -> True, ColorFunction -> "Rainbow",
  FrameTicks -> {{cft, cft}, {cft, cft}}]

enter image description here

With nbins = 25 we get

enter image description here

  • $\begingroup$ I take it that if I would like to increase the number of squares, I just alter that 5 in the second line? Also, I tried changing it to 25 and it looks good, but there are a lot of data ticks. Is it possible to maybe only show every second, or even third data tick? $\endgroup$
    – Lagiacrus
    Commented Feb 27, 2018 at 18:38
  • $\begingroup$ @Lagiacrus, right; changing 5 changes the number of bins. Re ticks, please see the updated version. $\endgroup$
    – kglr
    Commented Feb 27, 2018 at 20:17
  • $\begingroup$ So far the code appears to have been working well, but I just tried it for a similar situation in which the third element of the list can be either 0, 1, 2 or 3 (instead of the original 0 or 1) and in this case the binmeans never ends up with a value higher than 1. Is there a limit on the value that binmeans can take? $\endgroup$
    – Lagiacrus
    Commented Mar 3, 2018 at 12:57
  • $\begingroup$ @Lagiacrus, you can change {0, 2, 2} in the definition of binlists to {0,, 4, 4} if the third list can take values in {0,1,2,3}. $\endgroup$
    – kglr
    Commented Mar 3, 2018 at 14:00

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.