Python scatter plot - how to see number of entries per point -
i have data points repeat quite lot of entries (creating many overlapping (x,y) points) , i'm interested in knowing number of entries each point on graph. there easy way of doing (besides obvious of writing piece of code this?)
first of points list of tuples:
l = [(1,2), (0,0), (0,0), (1,2), (1,2), (3,4)]
i'm assuming you're reading data in file or something, , not hard-coding did above, modify import routine give list of tuples, or post-process imported data form list of tuples.
why going on tuples? because hashable, , therefore can used make set:
s = set(l) print (s) set([(1, 2), (0, 0), (3, 4)])
now have unique points in data, yay! ... how many times each repeated? can done counting them in list... or being lazy list count its-self using lists count method:
f = {} in list(s): f[i] = l.count(i)
now f dictionary containing frequency table each of our (x,y) points f.keys() give of unique locations, , dictionary contains how many times each point happened. need plot it:
from matplotlib.pyplot import figure, show fig = figure() sub = fig.add_subplot(111)
due fact using weird lists of tuples, we'll need use list comprehensions data format plot likes:
k = f.keys() xs = [i[0] in k] ys = [i[1] in k]
now plot nicely:
sub.plot(xs, ys, 'bo')
and plot can annotated our frequencies so:
for in k: sub.annotate(f[i], xy=i)
show plot:
show()
and this:
Comments
Post a Comment