# Plotting a frequency chart from a set of values

Hello,

I have got a list of results from my function already. And now, I need to visualise this information and plot a chart of how many times each particular value is repeated in a list. I know how to do a histogram, but it shows intervals of values, but not specific values. I need to access all the values afterwards as well and be able to work with them.

I have found also that I can count values using a command "count" list.count() if it is necessary to do it like this in my problem. I'd like to ask what is the code for such frequency chart (or plot of points)? And how do I need to define my variables? Would be much appreciated if someone could help in any way. Thank you very much.

Xenia

edit retag close merge delete

I have written a code now to count elements in a list named "results". It looks something like def count_element(): Y = [] for x in results: y = results.count() Y.append(y) return(y)

It seems it doesn't work properly, because it just shows 0 as a result. Can I define y axis in a similar way and then define x axis as values itself and make them interact?

( 2017-11-20 14:57:42 -0600 )edit

Sort by » oldest newest most voted

If you do not know in advance which numbers will belong to your list results, my favorite is the following:

sage: from collections import defaultdict
sage: d = defaultdict(int)
sage: for i in results:
....:     d[i] += 1
sage: points((i,d[i]) for i in d)


The problem with using the count method of lists is that, for each value in the list, the whole list is read, so it takes a linear amount of time. Hence, if all values are different, the global time will be quadratic.

With dictionary (as we did), the global time will be linear (in average, assuming the hash of your object does not create too much collisions, see https://wiki.python.org/moin/TimeComp...).

more

EDIT : Thierry's answer is _much_ better than mine, and is even _lazier_.

I'll leave my answer as a memento : _"Lazy" doesn't mean lazy enough to not look at Python's library documentation..._

If your numbers are ALL representable exactly as an R integer or float, R allows you to be lazy...

Let's be lazy :

sage: r.help("hist")

hist package:graphics R Documentation

Histograms

Description:

 The generic function ‘hist’ computes a histogram of the given data
values.  If ‘plot = TRUE’, the resulting object of class
‘"histogram"’ is plotted by ‘plot.histogram’, before it is
returned.


Usage:

 hist(x, ...)


(I abbreviated the screeenfuls of help...).

You may also want to plot yourself : R may regroup your results in more-or-less comparable bins, which mau or may not be what you want. This might come handy :

sage: results=[randint(-5,5) for i in range(100)] # Simulated results...
sage: H=r.table(results).sage() # Guarantees one count per distinct value in result.
## A mouthful, to be programmed.
sage: P=map(lambda a,b:[a,b], H.get("_Dimnames").get('DATA').values()[0],H.get('DATA'))
sage: P

[['-5', 12],
['-4', 12],
['-3', 12],
['-2', 15],
['-1', 11],
['0', 7],
['1', 10],
['2', 6],
['3', 4],
['4', 5],
['5', 6]]


points(P) is a first rough approximation of the graph you seek. Now, if you insist on a barchart,R may have some interesting possibilities...

Now, if your numbers are not representable exactly in R, I'd look for a Python library able to build a B-tree, whose nodes would store a tuple (key,number of occurrences). I don't have any idea right now, but I'd be surprised if such a library didn't exist. That would be in O(n*log(n))

HTH,

more

The tuple (key,number of occurrences) you are looking for is precisely the dictionary i described in my answer.

( 2017-11-21 01:42:19 -0600 )edit

Thierry, you're damn right. I tried to translate the R solution hardwired in my muscle memory....

( 2017-11-21 03:19:15 -0600 )edit