views:

62

answers:

4

I have a dense set of points in the plane. I want them colored so that points that are close to each other have the same color, and a different color if they're far away. For simplicity assume that there are, say, 5 different colors to choose from. Turns out I've not the slightest idea how to do that ..

I'm using Tkinter with Python, by the way

A: 

I'd start with identifying the concentrations of the spots in the plane. Find the centers of those agglomerations and assign them each unique color. Then for other spots you could simply calculate the color using the linear principle. For example, if one center is red and the other is yellow, a point somewhere in the middle would become orange.

I'd probably use some exponential function instead of a linear principle. This will keep the point groups more or less of the same color only giving a noticeable color change to far away points, or, to be more exact, to far away and somewhere in between points.

Developer Art
A: 

One approach is to go through your points and partition them into sets with a "center". Since you have 5 colours, you'll have 5 sets. You compare the distance of the new point from each of the centers and then put it in the same group as the closest one.

Each set corresponds to a different colour so you can just plot it after this partitioning is done.

Noufal Ibrahim
I like this idea - but I'm not sure how to choose those centers, and how to make sure each set has more or less the same number of points
ooboo
+2  A: 

If you can use whatever color you want, you can use that fact that colors are (almost) continuous. color the points according to their x,y coordinates, so you'll get as a side effect that close points will have a 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 is (x-min_x / (max_x-min_x)), so it would give 0 for the point with minimal x value, and 1 for point with maximal x value.

If you really need to use only a small number of colors and have close point have the exact same color, then you'll have to do some clustering on your data (K-means being a simple and widely used algorithm). After clustering, you just assign each point a color according to its cluster's id. Python has some good implementations, including scipy's clustering.

Ofri Raviv
yep. It seems that the tuning of the clustering is the most difficult part.
belisarius
Yeah, that's probably the best idea. I asked for distinct colors because I thought that would make things simpler, but turns out it is not so. I'll now have to read about clustering. Thanks :)
ooboo
A: 

The problem domain is the well-trodden cluster analysis and the Cluster suite with PyCluster is a good start.

msw