If you can use any color you want, you can use the fact that the colors are (almost) continuous. color the dots according to their x, y coordinates, so that you get as a side effect that the close dots will have somewhat similar color .
You can use something like
point.color(R,G,B) = ( point.normalized_x, 0.5, 1-point.normalized.y )
where normalized_x (x-min_x / (max_x-min_x)), so it will give 0 for a point with a minimum value of x and 1 for a point with a maximum value of x.
If you really need to use only a small number of colors and close the point have the same color , then you will need to do clustering according to your data ( K-means is a simple and widely used algorithm). After clustering, you simply assign a color to each point according to its cluster identifier. Python has some good implementations, including scipy clustering .
source share