Ask Your Question
2

How to plot histogram from list?

asked 10 years ago

Szabolcs gravatar image

updated 10 years ago

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?

Preview: (hide)

Comments

R is good for visualizing statistics. It is included in sage: http://ask.sagemath.org/question/7805/how-to-use-r-with-sage/

Thomas gravatar imageThomas ( 10 years ago )

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

Szabolcs gravatar imageSzabolcs ( 10 years ago )
1

As to your update on the cmap parameter, I think you may have to look for a cmap that does this. See http://matplotlib.org/examples/color/colormaps_reference.html and you can also design a custom cmap, see e.g. http://stackoverflow.com/questions/9707676/defining-a-discrete-colormap-for-imshow-in-matplotlib

kcrisman gravatar imagekcrisman ( 10 years ago )

2 Answers

Sort by » oldest newest most voted
1

answered 10 years ago

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.

Preview: (hide)
link

Comments

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 ( 10 years ago )

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 ( 10 years ago )
1

answered 10 years ago

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.

Preview: (hide)
link

Comments

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 ( 10 years ago )

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

kcrisman gravatar imagekcrisman ( 10 years ago )

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

Stats

Asked: 10 years ago

Seen: 4,950 times

Last updated: Aug 21 '14