Histogram

41 views
Skip to first unread message

Christophe Bal

unread,
Oct 9, 2014, 6:39:46 AM10/9/14
to sage-...@googlegroups.com
Hello.

I would like to plot an histogram. Does matplotlib be the only way ? 

C.

Christophe Bal

unread,
Oct 9, 2014, 6:45:44 AM10/9/14
to sage-...@googlegroups.com
I have just found 

T = stats.TimeSeries(datas)
T.plot_histogram(normalize=False,bins=len(datas))

No so friendly from a "young" student preparing the "BAC".

Is the a more pacific way ? ;-)

C.

Harald Schilly

unread,
Oct 9, 2014, 7:59:46 AM10/9/14
to sage-...@googlegroups.com
On Thu, Oct 9, 2014 at 12:39 PM, Christophe Bal <proj...@gmail.com> wrote:
> I would like to plot an histogram. Does matplotlib be the only way ?

Your question is very general, you can use R (hist, ggplot' hist),
Python (matplotlib (what's wrong with it?), pandas, ggplot) and Sage.
There are probably even more options, e.g. numpy/scipy also has
histogram routines.

What type of data is it?

-- harald

Christophe Bal

unread,
Oct 9, 2014, 8:05:21 AM10/9/14
to sage-...@googlegroups.com
Indeed, I was hoping a magic  histogram(datas) .

C.

Christophe Bal

unread,
Oct 9, 2014, 8:07:43 AM10/9/14
to sage-...@googlegroups.com
My datas are very simple : a list of numbers.

Harald Schilly

unread,
Oct 9, 2014, 8:54:58 AM10/9/14
to sage-...@googlegroups.com
On Thu, Oct 9, 2014 at 2:05 PM, Christophe Bal <proj...@gmail.com> wrote:
> Indeed, I was hoping a magic histogram(datas) .


In pandas, it's <series>.hist() ... e.g.

v = [4,3,5,2,3,2,2,5,7,8,7,8,6,5,7,5,4,5,6,5,6,5,4,3,4,3,4,3,3,3,4,3,3,2,3,2]
import matplotlib.pyplot as plt
plt.clf()
import pandas as pd
pd.Series(v).hist(bins=5).get_figure()

the plt.clf() and the .get_figure() is necessary, because sage
worksheets do not handle matplotlib plots "intellgently". (or I'm
doing something wrong, don't know)

-- harald

William Stein

unread,
Oct 9, 2014, 10:57:18 AM10/9/14
to sage-cloud, sage-support
On Thu, Oct 9, 2014 at 5:05 AM, Christophe Bal <proj...@gmail.com> wrote:
> Indeed, I was hoping a magic histogram(datas) .

This has come up dozens of times over the year. Somebody should
just make a top-level histogram command in Sage, which
options/functionality like [1]. The problem is that in 2006 when Alex
Clemesha decided to sit down and "implement math friendly 2d graphics"
he didn't get to histogram (though he did a massive amount of other
stuff), and for some reason most work after that has been
polishing/refactoring/improving the things he did do, rather than
adding new functions he missed.

For now, I think stats.TimeSeries(...) is the easiest way to make a
Sage-friendly 2d histogram. It certainly scales up to large input, at
least.

[1] http://reference.wolfram.com/language/ref/Histogram.html
> --
> You received this message because you are subscribed to the Google Groups
> "sage-cloud" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-cloud+...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sage-cloud/bdd7e6f4-2243-4450-857f-c78577c603a2%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
William Stein
Professor of Mathematics
University of Washington
http://wstein.org

William Stein

unread,
Oct 9, 2014, 12:56:31 PM10/9/14
to sage-cloud
I added support for more matplot-related plot types. Restart your
project server and this should work:

v = [1,1,1,1,1,1,1,4,3,5,2,3,2,2,5,7,8,7,8,6,5,7,5,4,5,6,5,6,5,4,3,4,3,4,3,3,3,4,3,3,2,3,2]
from pandas import Series
Series(v).hist(bins=5)


Note: pylab.clf() is no longer needed... it is now called at the
beginning of any cell. I played with pre-importing pandas, but it is
too slow to import all the time for everybody.

While I was at it, I changed R plotting (via %r mode) to use svg
instead of png for graphics, which looks a lot better.

-- William

Harald Schilly

unread,
Oct 9, 2014, 1:17:04 PM10/9/14
to sage-...@googlegroups.com
Ah, that's cool!
I remember that displaying matplotlib plots worked at some point in
the past, but I'm not sure. It's always a bit of a mystery to "figure"
out matplotlib plots ;-)

Don't bother about pandas, …

-- harald
> --
> You received this message because you are subscribed to the Google Groups "sage-cloud" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-cloud+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-cloud/CACLE5GB5BY8mCEpL1RaFNk7AXYs7BOpfttPjF-S2wma%2BAE4a3g%40mail.gmail.com.

Christophe Bal

unread,
Oct 9, 2014, 1:22:18 PM10/9/14
to sage-...@googlegroups.com
The pandas way is rather simple even if I prefer to do not use import with "young" students.

You received this message because you are subscribed to a topic in the Google Groups "sage-cloud" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sage-cloud/FFyruEkRaT4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sage-cloud+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sage-cloud/CAGG4CB5YR0sYB_Px%2BUtW%2BfMtHozgHM7AWUc4krmgWyXK%3DwousA%40mail.gmail.com.

William Stein

unread,
Oct 9, 2014, 2:07:28 PM10/9/14
to sage-cloud
On Thu, Oct 9, 2014 at 10:16 AM, Harald Schilly
<harald....@gmail.com> wrote:
> Ah, that's cool!
> I remember that displaying matplotlib plots worked at some point in
> the past, but I'm not sure. It's always a bit of a mystery to "figure"
> out matplotlib plots ;-)

Certain matplotlib plots showed. Unlike in Sage, there are different
class heierarchies of matplotlib plots,
and ways to deal with them, and I only supported, e.g., what was
output by your get_figure() method before.
There's a reason *that* worked instead of you having to save to a file
and salvus.file(...).

>
> Don't bother about pandas, ...
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-cloud/CAGG4CB5YR0sYB_Px%2BUtW%2BfMtHozgHM7AWUc4krmgWyXK%3DwousA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.



Reply all
Reply to author
Forward
0 new messages