# How to use logarithmic scale with histogram?

"Mika Oraj�rvi" <mikaorajarvi@...287...> writes:

This code does seem to draw some kind of histogram but it would be
much more usefull to have at least the y-scale as logarithmic. But I
haven't found a way to make the scale logarithmic.

According to the docstring of hist you can give it a keyword argument
of log=True to make the y axis logarithmic. However there is a slight
bug in that zero-height histogram bars like you have in your example
cause log(0) to be computed. Here's a quick fix:

···

------------------------------------------------------------------------
from pylab import *
x=0.000925,0.000879,0.000926,0.00088,0.001016,0.000931,0.000927,0.00088,\
0.000926,0.000926,0.000879,0.0009
n, bins = mlab.hist(x, 1000)
width = 0.9 * (bins-bins)
nz = nonzero(n)
bar(bins[nz], n[nz], width=width, log=True)
grid(True)
show()
------------------------------------------------------------------------

If the devs agree that this is a bug in hist, I can fix it in svn.

--
Jouni K. Sepp�nen
http://www.iki.fi/jks

Jouni K. Sepp�nen wrote:

"Mika Oraj�rvi" <mikaorajarvi@...287...> writes:

This code does seem to draw some kind of histogram but it would be
much more usefull to have at least the y-scale as logarithmic. But I
haven't found a way to make the scale logarithmic.

According to the docstring of hist you can give it a keyword argument
of log=True to make the y axis logarithmic. However there is a slight
bug in that zero-height histogram bars like you have in your example
cause log(0) to be computed. Here's a quick fix:

------------------------------------------------------------------------
from pylab import *
x=0.000925,0.000879,0.000926,0.00088,0.001016,0.000931,0.000927,0.00088,\
0.000926,0.000926,0.000879,0.0009
n, bins = mlab.hist(x, 1000)
width = 0.9 * (bins-bins)
nz = nonzero(n)
bar(bins[nz], n[nz], width=width, log=True)
grid(True)
show()
------------------------------------------------------------------------

If the devs agree that this is a bug in hist, I can fix it in svn.

I think it is already fixed. This works (drawing a sensible bar plot with a log scale despite bins with zero):

In :hist(rand(10), log=True)
Out:
(array([1, 0, 3, 1, 0, 1, 2, 1, 0, 1]),
array([ 0.00863515, 0.10200932, 0.19538349, 0.28875767, 0.38213184,
0.47550601, 0.56888018, 0.66225435, 0.75562852, 0.84900269]),
<a list of 10 Patch objects>)

Eric