By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

Plotting histograms

P: n/a
hi, I have some values(say from -a to a) stored in a vector and I want
to plot a histogram for those values. How can I get it done in python.
I have installed and imported the Matplotlib package but on executing
the code
[N,x]=hist(eig, 10) # make a histogram
I am getting an error saying "NameError: name 'hist' is not
defined".

Is there any other way to plot histograms over a given range?

thanks

Amit

Oct 17 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
am***********@gmail.com wrote:
hi, I have some values(say from -a to a) stored in a vector and I want
to plot a histogram for those values. How can I get it done in python.
I have installed and imported the Matplotlib package but on executing
the code
[N,x]=hist(eig, 10) # make a histogram
I am getting an error saying "NameError: name 'hist' is not
defined".
I presume what you did was something like this:

from matplotlib import pylab
[N,x] = hist(eig, 10)

What you actually want is this:

from matplotlib import pylab
[N,x] = pylab.hist(eig, 10)

Or, if you're at the interactive prompt (but remember that it is inadvisable to
do so in modules):

from matplotlib.pylab import *
[N,x] = hist(eig, 10)

You will probably want to review the section of the tutorial on importing
modules if you don't understand the differences.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Oct 17 '06 #2

P: n/a
On 16 Oct 2006 20:49:10 -0700, am***********@gmail.com
<am***********@gmail.comwrote:
hi, I have some values(say from -a to a) stored in a vector and I want
to plot a histogram for those values. How can I get it done in python.
I have installed and imported the Matplotlib package but on executing
the code
[N,x]=hist(eig, 10) # make a histogram
I am getting an error saying "NameError: name 'hist' is not
defined".
Use the statement 'from pylab import *' in the beginning of your program.

Others, of course, may find it more tasteful and Pythonic to do:

[N,x]=pylab.hist(eig, 10)

i.e., prefix it with the package name. Wouldn't want to clutter the
global namespace of your program after all.

Good luck with it then. I think I see a reference to advanced linear
algebra with 'eig' (Eigen-?) and I'm sure you understand that better
than I. :)

-- Theerasak
Oct 17 '06 #3

P: n/a
In article <ma**************************************@python.o rg>,
Robert Kern <ro*********@gmail.comwrote:
I presume what you did was something like this:

from matplotlib import pylab
[N,x] = hist(eig, 10)

What you actually want is this:

from matplotlib import pylab
[N,x] = pylab.hist(eig, 10)

Or, if you're at the interactive prompt (but remember that it is inadvisable
to
do so in modules):

from matplotlib.pylab import *
[N,x] = hist(eig, 10)

You will probably want to review the section of the tutorial on importing
modules if you don't understand the differences.

Is pylab part of matplotlib? I always thought it was the other way
around. I have a similar view of numpy as part of scipy. Maybe I'm
confused on the dependencies. I find it confusing in the examples
sometimes when the "bigger" package is imported (e.g. scipy) and then a
"subpackage" is also imported. Like this:

from scipi import *
from scipi import numpy

I know I've seen stuff like that, but I don't get it. The dependencies
are confusing to me.

I did a search of the tutorial on 'import' but didn't find the answer.

-- Lou Pecora (my views are my own) REMOVE THIS to email me.
Oct 17 '06 #4

P: n/a
Lou Pecora wrote:
In article <ma**************************************@python.o rg>,
Robert Kern <ro*********@gmail.comwrote:
>I presume what you did was something like this:

from matplotlib import pylab
[N,x] = hist(eig, 10)

What you actually want is this:

from matplotlib import pylab
[N,x] = pylab.hist(eig, 10)

Or, if you're at the interactive prompt (but remember that it is inadvisable
to
do so in modules):

from matplotlib.pylab import *
[N,x] = hist(eig, 10)

You will probably want to review the section of the tutorial on importing
modules if you don't understand the differences.

Is pylab part of matplotlib?
Yes.
I always thought it was the other way
around. I have a similar view of numpy as part of scipy.
It is not.
Maybe I'm
confused on the dependencies. I find it confusing in the examples
sometimes when the "bigger" package is imported (e.g. scipy) and then a
"subpackage" is also imported. Like this:

from scipi import *
from scipi import numpy
The latter would definitely be bad form if it worked. numpy is a package all by
itself and should be imported by itself.
I know I've seen stuff like that, but I don't get it. The dependencies
are confusing to me.
pylab is a module provided with matplotlib that exposes a nice interface for
certain purposes. Somewhat confusingly, it is provided in two places, as its own
module:

import pylab

and as a submodule in the matplotlib package:

from matplotlib import pylab

Both do the same thing. You get to ask John Hunter if you want to know the whys
and wherefores.

numpy is a package all by itself. scipy is a package all by itself although it
depends on numpy being installed. You cannot import numpy from scipy. The
dependency of scipy on numpy does *not* entail that scipy will provide numpy in
its namespace.

Sometimes packages/modules are sloppy and accidentally expose the modules that
they import. For example, if you had a module foo.py like this:

import bar
def dostuff():
pass

then foo.py depends on bar.py. One *could* also do this:

from foo import bar

However, as I said, this would be bad form. It is an accident that the bar
module is exposed there. It should not be imported from the foo module.

Naturally, there are exceptions. Sometimes some other module is deliberately
imported and intended to be exposed in that place. Hopefully, there is a comment
to that effect explaining that it was intentional.
I did a search of the tutorial on 'import' but didn't find the answer.
It certainly doesn't answer your questions, but it should answer the OP's if my
presumptions are correct. Importing a module like so:

import mymodule
from mypackage import myothermodule

does not take all of symbols in mymodule and myothermodule and place them in the
current namespace.

http://docs.python.org/tut/node8.html

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Oct 17 '06 #5

P: n/a
In article <ma**************************************@python.o rg>,
Robert Kern <ro*********@gmail.comwrote:

Is pylab part of matplotlib?

Yes.
I always thought it was the other way
around. I have a similar view of numpy as part of scipy.

It is not.
Maybe I'm
confused on the dependencies. I find it confusing in the examples
sometimes when the "bigger" package is imported (e.g. scipy) and then a
"subpackage" is also imported. Like this:

from scipi import *
from scipi import numpy

The latter would definitely be bad form if it worked. numpy is a package all
by
itself and should be imported by itself.
I know I've seen stuff like that, but I don't get it. The dependencies
are confusing to me.

pylab is a module provided with matplotlib that exposes a nice interface for
certain purposes. Somewhat confusingly, it is provided in two places, as its
own
module:

import pylab

and as a submodule in the matplotlib package:

from matplotlib import pylab

Both do the same thing. You get to ask John Hunter if you want to know the
whys
and wherefores.
[cut]

Got it. Thanks, Robert, for the quick tutorial. It's a lot clearer now.

Now, to just remember it.

By the way, from what I have seen so far they are beautiful packages.

The only problem I'm having is getting ipython to run. Not installed in
/usr/local/bin (although all other IPython files look to be installed in
/Library/Framewaorks/python...blah/site-packages). I'm still searching
the web sites for answers.

But all else seems to run pretty smoothly.

-- Lou Pecora (my views are my own) REMOVE THIS to email me.
Oct 17 '06 #6

P: n/a
Lou Pecora wrote:
The only problem I'm having is getting ipython to run. Not installed in
/usr/local/bin (although all other IPython files look to be installed in
/Library/Framewaorks/python...blah/site-packages). I'm still searching
the web sites for answers.
Create a file ~/.pydistutils.cfg with the following section (unindented):

[install]
install-scripts=/usr/local/bin

Now install ipython again, and the ipython script should be installed to
/usr/local/bin/. You may need to be root to do so.

For more information on that configuration file:

http://docs.python.org/inst/config-syntax.html

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Oct 17 '06 #7

P: n/a
In article <ma**************************************@python.o rg>,
Robert Kern <ro*********@gmail.comwrote:
Lou Pecora wrote:
The only problem I'm having is getting ipython to run. Not installed in
/usr/local/bin (although all other IPython files look to be installed in
/Library/Framewaorks/python..blah/site-packages). I'm still searching
the web sites for answers.

Create a file ~/.pydistutils.cfg with the following section (unindented):

[install]
install-scripts=/usr/local/bin

Now install ipython again, and the ipython script should be installed to
/usr/local/bin/. You may need to be root to do so.

For more information on that configuration file:

http://docs.python.org/inst/config-syntax.html
I assume you are telling me to install ipython from a tar distribution
using setup etc. I originally installed it from the SciPy Super Pack
which apparently put in the site packages, but neglected the script in
/usr/local/bin. Sound right?

On the ipython web site for MacOS X it says to do the following after
the install using python setup.py etc. in the extracted directories,

python*setup.py*install_scripts*--install-dir=/usr/local/bin

Is that what you are suggesting with the pydistutils.cfg ?

Sorry, if I'm dense on this, but I only partially grok the command line
install. Thanks for any help.

-- Lou Pecora (my views are my own) REMOVE THIS to email me.
Oct 17 '06 #8

P: n/a
Lou Pecora wrote:
In article <ma**************************************@python.o rg>,
Robert Kern <ro*********@gmail.comwrote:
>Lou Pecora wrote:
>>The only problem I'm having is getting ipython to run. Not installed in
/usr/local/bin (although all other IPython files look to be installed in
/Library/Framewaorks/python..blah/site-packages). I'm still searching
the web sites for answers.
Create a file ~/.pydistutils.cfg with the following section (unindented):

[install]
install-scripts=/usr/local/bin

Now install ipython again, and the ipython script should be installed to
/usr/local/bin/. You may need to be root to do so.

For more information on that configuration file:

http://docs.python.org/inst/config-syntax.html

I assume you are telling me to install ipython from a tar distribution
using setup etc. I originally installed it from the SciPy Super Pack
which apparently put in the site packages, but neglected the script in
/usr/local/bin. Sound right?
Indeed. The SuperPack is broken and does not contain the ipython script. It also
puts the documentation in the wrong place.

ipython is a pure-Python package and easy to install from source.
On the ipython web site for MacOS X it says to do the following after
the install using python setup.py etc. in the extracted directories,

python�setup.py�install_scripts�--install-dir=/usr/local/bin

Is that what you are suggesting with the pydistutils.cfg ?
Yes, it does the same thing, only it will apply to all packages (probably what
you want) and you only have to do it once instead of remembering to do it every
time.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Oct 17 '06 #9

P: n/a
In article <ma**************************************@python.o rg>,
Robert Kern <ro*********@gmail.comwrote:

I assume you are telling me to install ipython from a tar distribution
using setup etc. I originally installed it from the SciPy Super Pack
which apparently put in the site packages, but neglected the script in
/usr/local/bin. Sound right?

Indeed. The SuperPack is broken and does not contain the ipython script. It
also
puts the documentation in the wrong place.

ipython is a pure-Python package and easy to install from source.
Bingo! Now it is clear why I am having problems. I will install from
source as you suggest. Looks pretty straightforward.

Sorry to hear the SuperPack is broken. It's a nice idea for us users.
Other packages seem to be working fine. Any chance it will be fixed?
On the ipython web site for MacOS X it says to do the following after
the install using python setup.py etc. in the extracted directories,

python?setup.py?install_scripts?--install-dir=/usr/local/bin

Is that what you are suggesting with the pydistutils.cfg ?

Yes, it does the same thing, only it will apply to all packages (probably
what
you want) and you only have to do it once instead of remembering to do it
every
time.
Robert, thanks for the insight and the solution.

-- Lou Pecora (my views are my own) REMOVE THIS to email me.
Oct 17 '06 #10

P: n/a
Thanks Robert,

My previous problem is solved(I was using 'from matplotlib.pylab import
*') but now I am facing another problem. I want to plot the histogram
of eigenvalues calculated and I am using the following code:
__________________________________________________ _____________________
import numpy
from matplotlib import pylab

n=100
ra = numpy.random
la = numpy.linalg

A = ra.standard_normal((n,n))
S = (A + numpy.transpose(A))/(2*n^(1/2))

eig = la.eigvals(S)

[N,x]=pylab.hist(eig, 10) # make a histogram
-------------------------------------------------------------------------------------------------------------

But again it is giving some error, which is given below:

File "C:\Documents and Settings\amitsoni\Desktop\New
Folder\wignerpython", line 15, in <module>
[N,x]=pylab.hist(eig, 10) # make a histogram
ValueError: too many values to unpack

Can anyone help me out with this??

Thanks
Amit

Robert Kern wrote:
am***********@gmail.com wrote:
hi, I have some values(say from -a to a) stored in a vector and I want
to plot a histogram for those values. How can I get it done in python.
I have installed and imported the Matplotlib package but on executing
the code
[N,x]=hist(eig, 10) # make a histogram
I am getting an error saying "NameError: name 'hist' is not
defined".

I presume what you did was something like this:

from matplotlib import pylab
[N,x] = hist(eig, 10)

What you actually want is this:

from matplotlib import pylab
[N,x] = pylab.hist(eig, 10)

Or, if you're at the interactive prompt (but remember that it is inadvisable to
do so in modules):

from matplotlib.pylab import *
[N,x] = hist(eig, 10)

You will probably want to review the section of the tutorial on importing
modules if you don't understand the differences.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
Oct 18 '06 #11

P: n/a
am***********@gmail.com wrote:
Thanks Robert,

My previous problem is solved(I was using 'from matplotlib.pylab import
*') but now I am facing another problem. I want to plot the histogram
of eigenvalues calculated and I am using the following code:
__________________________________________________ _____________________
import numpy
from matplotlib import pylab

n=100
ra = numpy.random
la = numpy.linalg

A = ra.standard_normal((n,n))
S = (A + numpy.transpose(A))/(2*n^(1/2))
Note that this line won't do what you think it does. First, one integer divided
by another integer returns an integer, so (1/2) == 0. Also, ^ is not
exponentiation but bitwise XOR. Use ** for exponentiation. However, in this
case, you should use numpy.sqrt().
eig = la.eigvals(S)

[N,x]=pylab.hist(eig, 10) # make a histogram
-------------------------------------------------------------------------------------------------------------

But again it is giving some error, which is given below:

File "C:\Documents and Settings\amitsoni\Desktop\New
Folder\wignerpython", line 15, in <module>
[N,x]=pylab.hist(eig, 10) # make a histogram
ValueError: too many values to unpack

Can anyone help me out with this??
pylab.hist() does not return two values, it returns three. Sorry I didn't catch
that earlier.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Oct 18 '06 #12

P: n/a
am***********@gmail.com wrote:
hi, I have some values(say from -a to a) stored in a vector and I want
to plot a histogram for those values. How can I get it done in python.
I have installed and imported the Matplotlib package but on executing
the code
[N,x]=hist(eig, 10) # make a histogram
I am getting an error saying "NameError: name 'hist' is not
defined".

Is there any other way to plot histograms over a given range?
>># create random vector
.... from random import randrange
>>a = 5
v = [randrange(-a, a+1) for i in xrange(100)]

# print histogram
.... for i in range(-a, a+1):
.... print "%+d %s" % (i, '*' * v.count(i))
....
-5 *********
-4 *****
-3 *****
-2 **********
-1 **********
+0 *****
+1 ************
+2 *******
+3 *****
+4 ********************
+5 ************
:)
--
Roberto Bonvallet
Oct 18 '06 #13

P: n/a
Thanks for the replies ... its perfect now ... but just one more thing
.... how can I plot another function(a semi circle) in the same
histogram?

thanks

amit
Robert Kern wrote:
am***********@gmail.com wrote:
Thanks Robert,

My previous problem is solved(I was using 'from matplotlib.pylab import
*') but now I am facing another problem. I want to plot the histogram
of eigenvalues calculated and I am using the following code:
__________________________________________________ _____________________
import numpy
from matplotlib import pylab

n=100
ra = numpy.random
la = numpy.linalg

A = ra.standard_normal((n,n))
S = (A + numpy.transpose(A))/(2*n^(1/2))

Note that this line won't do what you think it does. First, one integer divided
by another integer returns an integer, so (1/2) == 0. Also, ^ is not
exponentiation but bitwise XOR. Use ** for exponentiation. However, in this
case, you should use numpy.sqrt().
eig = la.eigvals(S)

[N,x]=pylab.hist(eig, 10) # make a histogram
-------------------------------------------------------------------------------------------------------------

But again it is giving some error, which is given below:

File "C:\Documents and Settings\amitsoni\Desktop\New
Folder\wignerpython", line 15, in <module>
[N,x]=pylab.hist(eig, 10) # make a histogram
ValueError: too many values to unpack

Can anyone help me out with this??

pylab.hist() does not return two values, it returns three. Sorry I didn't catch
that earlier.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Ec
Oct 19 '06 #14

P: n/a
am***********@gmail.com wrote:
Thanks for the replies ... its perfect now ... but just one more thing
... how can I plot another function(a semi circle) in the same
histogram?
Just call the appropriate plotting function after you plot the histogram. By
default, the second plot will go into the same figure as the first. Something
like the following should suffice:

x = numpy.linspace(-1.0, 1.0, 201)
y = numpy.sqrt(1.0 - x*x)
pylab.plot(x, y, 'k-')

Look at the matplotlib documentation for more information. You will probably
also want to ask future questions on the matplotlib-users mailing list instead
of here.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Oct 19 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.