ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 21 Nov 2017 03:19:15 -0600Plotting a frequency chart from a set of valueshttp://ask.sagemath.org/question/39677/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.
XeniaMon, 20 Nov 2017 12:12:52 -0600http://ask.sagemath.org/question/39677/plotting-a-frequency-chart-from-a-set-of-values/Comment by Xenia for <p>Hello,</p>
<p>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.</p>
<hr>
<p>I have found also that I can count values using a command "count"
<code>list.count()</code> 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.</p>
<p>Xenia</p>
http://ask.sagemath.org/question/39677/plotting-a-frequency-chart-from-a-set-of-values/?comment=39683#post-id-39683I 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?Mon, 20 Nov 2017 14:57:42 -0600http://ask.sagemath.org/question/39677/plotting-a-frequency-chart-from-a-set-of-values/?comment=39683#post-id-39683Answer by tmonteil for <p>Hello,</p>
<p>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.</p>
<hr>
<p>I have found also that I can count values using a command "count"
<code>list.count()</code> 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.</p>
<p>Xenia</p>
http://ask.sagemath.org/question/39677/plotting-a-frequency-chart-from-a-set-of-values/?answer=39684#post-id-39684If 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/TimeComplexity#dict).Mon, 20 Nov 2017 15:08:15 -0600http://ask.sagemath.org/question/39677/plotting-a-frequency-chart-from-a-set-of-values/?answer=39684#post-id-39684Answer by Emmanuel Charpentier for <p>Hello,</p>
<p>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.</p>
<hr>
<p>I have found also that I can count values using a command "count"
<code>list.count()</code> 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.</p>
<p>Xenia</p>
http://ask.sagemath.org/question/39677/plotting-a-frequency-chart-from-a-set-of-values/?answer=39685#post-id-39685**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,Mon, 20 Nov 2017 16:24:35 -0600http://ask.sagemath.org/question/39677/plotting-a-frequency-chart-from-a-set-of-values/?answer=39685#post-id-39685Comment by Emmanuel Charpentier for <p><strong>EDIT :</strong> Thierry's answer is _much_ better than mine, and is even _lazier_.</p>
<p>I'll leave my answer as a memento : _"Lazy" doesn't mean lazy enough to not look at Python's library documentation..._</p>
<p>If your numbers are ALL representable exactly as an R integer or float, R allows you to be lazy...</p>
<p>Let's be lazy :</p>
<blockquote>
<p>sage: r.help("hist")</p>
<p>hist package:graphics
R Documentation</p>
<p>Histograms</p>
<p>Description:</p>
<pre><code> 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.
</code></pre>
<p>Usage:</p>
<pre><code> hist(x, ...)
</code></pre>
</blockquote>
<p>(I abbreviated the screeenfuls of help...).</p>
<p>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 :</p>
<pre><code>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]]
</code></pre>
<p><code>points(P)</code> is a first rough approximation of the graph you seek. Now, if you insist on a barchart,R may have some interesting possibilities...</p>
<p>Now, if your numbers are <em>not</em> 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 <code>O(n*log(n))</code></p>
<p>HTH,</p>
http://ask.sagemath.org/question/39677/plotting-a-frequency-chart-from-a-set-of-values/?comment=39689#post-id-39689Thierry, you're damn right. I tried to translate the R solution hardwired in my muscle memory....Tue, 21 Nov 2017 03:19:15 -0600http://ask.sagemath.org/question/39677/plotting-a-frequency-chart-from-a-set-of-values/?comment=39689#post-id-39689Comment by tmonteil for <p><strong>EDIT :</strong> Thierry's answer is _much_ better than mine, and is even _lazier_.</p>
<p>I'll leave my answer as a memento : _"Lazy" doesn't mean lazy enough to not look at Python's library documentation..._</p>
<p>If your numbers are ALL representable exactly as an R integer or float, R allows you to be lazy...</p>
<p>Let's be lazy :</p>
<blockquote>
<p>sage: r.help("hist")</p>
<p>hist package:graphics
R Documentation</p>
<p>Histograms</p>
<p>Description:</p>
<pre><code> 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.
</code></pre>
<p>Usage:</p>
<pre><code> hist(x, ...)
</code></pre>
</blockquote>
<p>(I abbreviated the screeenfuls of help...).</p>
<p>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 :</p>
<pre><code>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]]
</code></pre>
<p><code>points(P)</code> is a first rough approximation of the graph you seek. Now, if you insist on a barchart,R may have some interesting possibilities...</p>
<p>Now, if your numbers are <em>not</em> 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 <code>O(n*log(n))</code></p>
<p>HTH,</p>
http://ask.sagemath.org/question/39677/plotting-a-frequency-chart-from-a-set-of-values/?comment=39688#post-id-39688The tuple (key,number of occurrences) you are looking for is precisely the dictionary i described in my answer.Tue, 21 Nov 2017 01:42:19 -0600http://ask.sagemath.org/question/39677/plotting-a-frequency-chart-from-a-set-of-values/?comment=39688#post-id-39688