ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 21 Aug 2014 22:19:43 +0200How to plot histogram from list?https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/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](http://1.bp.blogspot.com/--uLzBni5Dz0/TfoHTL4caOI/AAAAAAAAC3k/QLvvkAEb9zo/s1600/twodhist.jpg)? (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?
Thu, 21 Aug 2014 05:00:27 +0200https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/Comment by Szabolcs for <p>Given a list of numbers, such as <code>[5,3,6,1,...]</code>, how can I create a histogram from them and show it directly in the notebook interface?</p>
<p><strong>Clarification:</strong> 5, 3, 6, 1, ... should not be used for the heights of bars. Instead, the data should be binned first to create a histogram.</p>
<hr/>
<p>Given a list of pairs, such as <code>[[2,3], [4,5], ...]</code>, how can I create a 2D histogram form them and show it as a plot similar to <a href="http://1.bp.blogspot.com/--uLzBni5Dz0/TfoHTL4caOI/AAAAAAAAC3k/QLvvkAEb9zo/s1600/twodhist.jpg">this</a>? (I.e. clearly distinguish zero counts from small non-zero counts, and show it as a matrix plot, not as a 3D bar chart.)</p>
<p><strong>Clarification:</strong> How can I use a colour scheme in <code>matrix_plot</code> that will clearly differentiate between zero and non-zero values? E.g. use white for zero and some colour gradient for non-zero. <code>0</code> and <code>0.0001</code> should show clearly differently. How do I achieve this with the <code>cmap</code> parameter?</p>
https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/?comment=23879#post-id-23879@Thomas Thanks, that's probably what I will do.Thu, 21 Aug 2014 15:44:35 +0200https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/?comment=23879#post-id-23879Comment by kcrisman for <p>Given a list of numbers, such as <code>[5,3,6,1,...]</code>, how can I create a histogram from them and show it directly in the notebook interface?</p>
<p><strong>Clarification:</strong> 5, 3, 6, 1, ... should not be used for the heights of bars. Instead, the data should be binned first to create a histogram.</p>
<hr/>
<p>Given a list of pairs, such as <code>[[2,3], [4,5], ...]</code>, how can I create a 2D histogram form them and show it as a plot similar to <a href="http://1.bp.blogspot.com/--uLzBni5Dz0/TfoHTL4caOI/AAAAAAAAC3k/QLvvkAEb9zo/s1600/twodhist.jpg">this</a>? (I.e. clearly distinguish zero counts from small non-zero counts, and show it as a matrix plot, not as a 3D bar chart.)</p>
<p><strong>Clarification:</strong> How can I use a colour scheme in <code>matrix_plot</code> that will clearly differentiate between zero and non-zero values? E.g. use white for zero and some colour gradient for non-zero. <code>0</code> and <code>0.0001</code> should show clearly differently. How do I achieve this with the <code>cmap</code> parameter?</p>
https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/?comment=23883#post-id-23883As 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-matplotlibThu, 21 Aug 2014 15:55:47 +0200https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/?comment=23883#post-id-23883Comment by Thomas for <p>Given a list of numbers, such as <code>[5,3,6,1,...]</code>, how can I create a histogram from them and show it directly in the notebook interface?</p>
<p><strong>Clarification:</strong> 5, 3, 6, 1, ... should not be used for the heights of bars. Instead, the data should be binned first to create a histogram.</p>
<hr/>
<p>Given a list of pairs, such as <code>[[2,3], [4,5], ...]</code>, how can I create a 2D histogram form them and show it as a plot similar to <a href="http://1.bp.blogspot.com/--uLzBni5Dz0/TfoHTL4caOI/AAAAAAAAC3k/QLvvkAEb9zo/s1600/twodhist.jpg">this</a>? (I.e. clearly distinguish zero counts from small non-zero counts, and show it as a matrix plot, not as a 3D bar chart.)</p>
<p><strong>Clarification:</strong> How can I use a colour scheme in <code>matrix_plot</code> that will clearly differentiate between zero and non-zero values? E.g. use white for zero and some colour gradient for non-zero. <code>0</code> and <code>0.0001</code> should show clearly differently. How do I achieve this with the <code>cmap</code> parameter?</p>
https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/?comment=23875#post-id-23875R is good for visualizing statistics. It is included in sage: http://ask.sagemath.org/question/7805/how-to-use-r-with-sage/Thu, 21 Aug 2014 11:20:21 +0200https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/?comment=23875#post-id-23875Answer by kcrisman for <p>Given a list of numbers, such as <code>[5,3,6,1,...]</code>, how can I create a histogram from them and show it directly in the notebook interface?</p>
<p><strong>Clarification:</strong> 5, 3, 6, 1, ... should not be used for the heights of bars. Instead, the data should be binned first to create a histogram.</p>
<hr/>
<p>Given a list of pairs, such as <code>[[2,3], [4,5], ...]</code>, how can I create a 2D histogram form them and show it as a plot similar to <a href="http://1.bp.blogspot.com/--uLzBni5Dz0/TfoHTL4caOI/AAAAAAAAC3k/QLvvkAEb9zo/s1600/twodhist.jpg">this</a>? (I.e. clearly distinguish zero counts from small non-zero counts, and show it as a matrix plot, not as a 3D bar chart.)</p>
<p><strong>Clarification:</strong> How can I use a colour scheme in <code>matrix_plot</code> that will clearly differentiate between zero and non-zero values? E.g. use white for zero and some colour gradient for non-zero. <code>0</code> and <code>0.0001</code> should show clearly differently. How do I achieve this with the <code>cmap</code> parameter?</p>
https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/?answer=23881#post-id-23881There are several ways to plot histograms in Sage, none obvious.
- Time Series in Sage have them - see [here](http://www.sagemath.org/doc/prep/Quickstarts/Statistics-and-Distributions.html#distributions) 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](http://www.safaribooksonline.com/library/view/sage-beginners-guide/9781849514460/ch06s17.html) 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](http://trac.sagemath.org/ticket/9671) into Sage.Thu, 21 Aug 2014 15:51:09 +0200https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/?answer=23881#post-id-23881Comment by kcrisman for <p>There are several ways to plot histograms in Sage, none obvious. </p>
<ul>
<li>Time Series in Sage have them - see <a href="http://www.sagemath.org/doc/prep/Quickstarts/Statistics-and-Distributions.html#distributions">here</a> for an example.</li>
<li>R, as mentioned above.</li>
<li>Matplotlib can do them directly as well, you just have to know how to do their plots in Sage. The <a href="http://www.safaribooksonline.com/library/view/sage-beginners-guide/9781849514460/ch06s17.html">Sage beginners guide</a> by Craig Finch (not part of the development team) has some very nice examples.</li>
</ul>
<p>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 <a href="http://trac.sagemath.org/ticket/9671">Trac 9671</a> into Sage.</p>
https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/?comment=23894#post-id-23894If 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.Thu, 21 Aug 2014 22:19:43 +0200https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/?comment=23894#post-id-23894Comment by Szabolcs for <p>There are several ways to plot histograms in Sage, none obvious. </p>
<ul>
<li>Time Series in Sage have them - see <a href="http://www.sagemath.org/doc/prep/Quickstarts/Statistics-and-Distributions.html#distributions">here</a> for an example.</li>
<li>R, as mentioned above.</li>
<li>Matplotlib can do them directly as well, you just have to know how to do their plots in Sage. The <a href="http://www.safaribooksonline.com/library/view/sage-beginners-guide/9781849514460/ch06s17.html">Sage beginners guide</a> by Craig Finch (not part of the development team) has some very nice examples.</li>
</ul>
<p>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 <a href="http://trac.sagemath.org/ticket/9671">Trac 9671</a> into Sage.</p>
https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/?comment=23887#post-id-23887Sorry 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.Thu, 21 Aug 2014 19:08:53 +0200https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/?comment=23887#post-id-23887Answer by FrédéricC for <p>Given a list of numbers, such as <code>[5,3,6,1,...]</code>, how can I create a histogram from them and show it directly in the notebook interface?</p>
<p><strong>Clarification:</strong> 5, 3, 6, 1, ... should not be used for the heights of bars. Instead, the data should be binned first to create a histogram.</p>
<hr/>
<p>Given a list of pairs, such as <code>[[2,3], [4,5], ...]</code>, how can I create a 2D histogram form them and show it as a plot similar to <a href="http://1.bp.blogspot.com/--uLzBni5Dz0/TfoHTL4caOI/AAAAAAAAC3k/QLvvkAEb9zo/s1600/twodhist.jpg">this</a>? (I.e. clearly distinguish zero counts from small non-zero counts, and show it as a matrix plot, not as a 3D bar chart.)</p>
<p><strong>Clarification:</strong> How can I use a colour scheme in <code>matrix_plot</code> that will clearly differentiate between zero and non-zero values? E.g. use white for zero and some colour gradient for non-zero. <code>0</code> and <code>0.0001</code> should show clearly differently. How do I achieve this with the <code>cmap</code> parameter?</p>
https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/?answer=23874#post-id-23874For 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.Thu, 21 Aug 2014 09:16:15 +0200https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/?answer=23874#post-id-23874Comment by Szabolcs for <p>For the first question, you can use</p>
<pre><code> sage: bar_chart([5,3,6,1], width=1)
</code></pre>
<p>For the second question, maybe something like that</p>
<pre><code>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)
</code></pre>
<p>You should then use the options of matrix_plot to change the colors as you want.</p>
https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/?comment=23880#post-id-23880Thanks! 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?Thu, 21 Aug 2014 15:45:31 +0200https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/?comment=23880#post-id-23880Comment by kcrisman for <p>For the first question, you can use</p>
<pre><code> sage: bar_chart([5,3,6,1], width=1)
</code></pre>
<p>For the second question, maybe something like that</p>
<pre><code>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)
</code></pre>
<p>You should then use the options of matrix_plot to change the colors as you want.</p>
https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/?comment=23882#post-id-23882Binning should be possible with any proper histogram interface, I think.Thu, 21 Aug 2014 15:53:30 +0200https://ask.sagemath.org/question/23870/how-to-plot-histogram-from-list/?comment=23882#post-id-23882