Ask Your Question
2

How to plot histogram from list?

asked 2014-08-21 05:00:27 +0200

Szabolcs gravatar image

updated 2014-08-21 15:41:37 +0200

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

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 ( 2014-08-21 11:20:21 +0200 )edit

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

Szabolcs gravatar imageSzabolcs ( 2014-08-21 15:44:35 +0200 )edit
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 ( 2014-08-21 15:55:47 +0200 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2014-08-21 15:51:09 +0200

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

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 ( 2014-08-21 19:08:53 +0200 )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 22:19:43 +0200 )edit
1

answered 2014-08-21 09:16:15 +0200

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

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 ( 2014-08-21 15:45:31 +0200 )edit

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

kcrisman gravatar imagekcrisman ( 2014-08-21 15:53:30 +0200 )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

Stats

Asked: 2014-08-21 05:00:27 +0200

Seen: 4,764 times

Last updated: Aug 21 '14