Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

## matplotlib-users

 [Matplotlib-users] How to use logarithmic scale with histogram? From: - 2007-02-06 14:33:57 Attachments: Message as HTML ```Hi! I'm trying to generate some kind of "distribution" view / histogram of decimal numbers, i.e. i want the graph to indicate exactly how many times any given decimal number occurs in "x". As an example, i've set the values to tuple "x" as seen in the code snipplet below. In reality there will be at least couple of thousand decimal values or more in the tuple (x in this example) and values will be retrieved from file etc. 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. I've tried to use semilogy/semilogx/loglog but with no success. --------------- 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, patches = hist(x, 1000) l = plot(bins, n, 'r--') grid(True) show() ---------------- regards, Mika ```
 Re: [Matplotlib-users] How to use logarithmic scale with histogram? From: Matthias Michler - 2007-02-12 12:16:14 ```Hi Mika,=20 Hi everybody, I'm not sure I really understand your problem, but I attached my proposal /= =20 solution, so you can think about it or maybe describe your problem once mor= e. Much success, Matthias Michler >--------------------------------------------------------------------------= =2D----------------------- from pylab import figure, subplot, grid, close, show,\ hist, plot, semilogy, nonzero x =3D 0.000925, 0.000879, 0.000926, 0.00088, 0.001016, 0.000931, 0.000927, \ 0.00088, 0.000926, 0.000926, 0.000879, 0.0009 figure(0) # make histogram in figure 0 n, bins, patches =3D hist(x, 1000, align=3D'center') close(0) # close figure to delete plot # instance of hist index =3D nonzero(n) # to solve problems mit zero # values in log scale figure(1) ax =3D subplot(111) ax.set_yscale('log') l1 =3D plot(bins[index], n[index], ls=3D'',marker =3D'x', ms=3D15, mew=3D3,= c=3D'r') grid(True) figure(2) l2 =3D semilogy(bins[index], n[index], ls=3D'',marker =3D'x', ms=3D15, mew= =3D3, c=3D'r') grid(True) show() >--------------------------------------------------------------------------= =2D------------------------ On Tuesday 06 February 2007 15:33, Mika Oraj=E4rvi wrote: > Hi! > I'm trying to generate some kind of "distribution" view / histogram > of decimal numbers, i.e. i want the graph to indicate exactly how many > times any given decimal number occurs in "x". As an example, i've set the > values to tuple "x" as seen in the code snipplet below. In reality there > will be at least couple of thousand decimal values or more in the tuple (x > in this example) and values will be retrieved from file etc. This code do= es > 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 ma= ke > the scale logarithmic. I've tried to use semilogy/semilogx/loglog but with > no success. > > --------------- > x=3D0.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, patches =3D hist(x, 1000) > l =3D plot(bins, n, 'r--') > grid(True) > show() > ---------------- > > regards, Mika ```
 Re: [Matplotlib-users] How to use logarithmic scale with histogram? From: - 2007-02-12 15:11:31 ```"Mika Orajärvi" 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[1]-bins[0]) 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 ```
 Re: [Matplotlib-users] How to use logarithmic scale with histogram? From: Eric Firing - 2007-02-12 19:57:14 ```Jouni K. Seppänen wrote: > "Mika Orajärvi" 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[1]-bins[0]) > 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 [3]:hist(rand(10), log=True) Out[3]: (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]), ) Eric ```