Alona Levy Alona Levy - 2 years ago 82
Python Question

Python scatter plot - how to see number of entries per point

I have data points that repeat themselves for quite a lot of entries (creating many overlapping (x,y) points) and I'm really interested in knowing the number of entries for each point on the graph. Is there an easy way of doing this (besides the obvious of writing a piece of code that does this?)

Answer Source

First of all get your points into a list of tuples:

L = [(1,2), (0,0), (0,0), (1,2), (1,2), (3,4)]

I'm assuming you're reading your data in from a file or something, and not hard-coding it like I did above, so modify your import routine to give you a list of tuples, or post-process your imported data to form your list of tuples.

Why am I going on about tuples? Because they are hashable, and therefore can be used to make a set:

S = set(L)
print (S)
set([(1, 2), (0, 0), (3, 4)])

Now we have all the unique points in the data, YAY! ... but how many times is each repeated? That can be done by counting them in the list... Or being too lazy to do that get the list to count its-self using the lists count method:

F = {}
for i in list(S):
    F[i] = L.count(i)

Now F is a dictionary containing a frequency table for each of our (X,Y) points F.keys() will give you all of the unique locations, and the dictionary contains how many times each point happened. Now all we need to do is plot it:

from matplotlib.pyplot import figure, show
fig = figure()
sub = fig.add_subplot(111)

Due to the fact that we are using weird lists of tuples, we'll need to use some list comprehensions to get the data back into a format that plot likes:

K = F.keys()
Xs = [i[0] for i in K]
Ys = [i[1] for i in K]

Now it will plot nicely:

sub.plot(Xs, Ys, 'bo')

and the plot can be annotated with our frequencies like so:

for i in K:
    sub.annotate(F[i], xy=i)

Show the plot:

show()

And you will get something like this: Resulting plot

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download