440,621 Members | 1,077 Online 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
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

 P: n/a In article , Robert Kern

 P: n/a In article , Robert Kern

 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 , Robert Kern

 P: n/a Lou Pecora wrote: In article , Robert Kern 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 searchingthe web sites for answers. Create a file ~/.pydistutils.cfg with the following section (unindented): [install] install-scripts=/usr/local/binNow 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 , Robert Kern

 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 [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 [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 = 5v = [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 [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. 