Ask Your Question

How to plot histogram from list?

asked 2014-08-20 22:00:27 -0500

Szabolcs gravatar image

updated 2014-08-21 08:41:37 -0500

Given a list of numbers, such as [5,3,6,1,...], how can I create a histogram from them and show it directly in the notebook interface?

Clarification: 5, 3, 6, 1, ... should not be used for the heights of bars. Instead, the data should be binned first to create a histogram.

Given a list of pairs, such as [[2,3], [4,5], ...], how can I create a 2D histogram form them and show it as a plot similar to this? (I.e. clearly distinguish zero counts from small non-zero counts, and show it as a matrix plot, not as a 3D bar chart.)

Clarification: How can I use a colour scheme in matrix_plot that will clearly differentiate between zero and non-zero values? E.g. use white for zero and some colour gradient for non-zero. 0 and 0.0001 should show clearly differently. How do I achieve this with the cmap parameter?

edit retag flag offensive close merge delete


R is good for visualizing statistics. It is included in sage:

Thomas gravatar imageThomas ( 2014-08-21 04:20:21 -0500 )edit

@Thomas Thanks, that's probably what I will do.

Szabolcs gravatar imageSzabolcs ( 2014-08-21 08:44:35 -0500 )edit

As to your update on the cmap parameter, I think you may have to look for a cmap that does this. See and you can also design a custom cmap, see e.g.

kcrisman gravatar imagekcrisman ( 2014-08-21 08:55:47 -0500 )edit

2 answers

Sort by » oldest newest most voted

answered 2014-08-21 08:51:09 -0500

kcrisman gravatar image

There are several ways to plot histograms in Sage, none obvious.

  • Time Series in Sage have them - see here for an example.
  • R, as mentioned above.
  • Matplotlib can do them directly as well, you just have to know how to do their plots in Sage. The Sage beginners guide by Craig Finch (not part of the development team) has some very nice examples.

I think there are even more ways but I don't have them "on me" right now. Naturally, the correct answer is for me or someone else to get off my duff and finish getting Trac 9671 into Sage.

edit flag offensive delete link more


Sorry about the basic question, but when using Matplotlib, does the figure actually get saved to disk by `plt.savefig`? If yes, where, and do I need to clean it up afterwards? I do not want to keep these figures except as part of the notebook.

Szabolcs gravatar imageSzabolcs ( 2014-08-21 12:08:53 -0500 )edit

If you are only talking about the notebook, you don't have to clean them up. (I don't think?) They are inside of the directory of the cell you create them in (you can see this via a tedious waltz through the tree of files in the notebook directory). I don't think they should save to your home directory or anything like that.

kcrisman gravatar imagekcrisman ( 2014-08-21 15:19:43 -0500 )edit

answered 2014-08-21 02:16:15 -0500

FrédéricC gravatar image

For the first question, you can use

 sage: bar_chart([5,3,6,1], width=1)

For the second question, maybe something like that

sage: data = [[4,3],[5,6],[7,2],[5,6]]
sage: m = matrix(10,10)
sage: for x,y in data:
....:     m[x,y] += 1
....: matrix_plot(m)

You should then use the options of matrix_plot to change the colors as you want.

edit flag offensive delete link more


Thanks! Could you please look at the clarifications I put in the question? Is it difficult to do these things with Sage? Do you also recommend I just use the R interface, like Thomas did?

Szabolcs gravatar imageSzabolcs ( 2014-08-21 08:45:31 -0500 )edit

Binning should be possible with any proper histogram interface, I think.

kcrisman gravatar imagekcrisman ( 2014-08-21 08:53:30 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-08-20 22:00:27 -0500

Seen: 1,317 times

Last updated: Aug 21 '14