Plotting histograms

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • amitsoni.1984@gmail.com

    #1

    Plotting histograms

    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

  • Robert Kern

    #2
    Re: Plotting histograms

    amitsoni.1984@g mail.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.pyla b 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

    Comment

    • Theerasak Photha

      #3
      Re: Plotting histograms

      On 16 Oct 2006 20:49:10 -0700, amitsoni.1984@g mail.com
      <amitsoni.1984@ 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

      Comment

      • Lou Pecora

        #4
        pylab package dependencies (was: Plotting histograms)

        In article <mailman.604.11 61057667.11739. python-list@python.org >,
        Robert Kern <robert.kern@gm ail.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.pyla b 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.

        Comment

        • Robert Kern

          #5
          Re: pylab package dependencies

          Lou Pecora wrote:
          In article <mailman.604.11 61057667.11739. python-list@python.org >,
          Robert Kern <robert.kern@gm ail.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.pyla b 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.



          --
          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

          Comment

          • Lou Pecora

            #6
            Re: pylab package dependencies

            In article <mailman.650.11 61106780.11739. python-list@python.org >,
            Robert Kern <robert.kern@gm ail.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.

            Comment

            • Robert Kern

              #7
              Re: pylab package dependencies

              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.cf g 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:



              --
              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

              Comment

              • Lou Pecora

                #8
                Re: pylab package dependencies

                In article <mailman.654.11 61110013.11739. python-list@python.org >,
                Robert Kern <robert.kern@gm ail.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.cf g 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.

                Comment

                • Robert Kern

                  #9
                  Re: pylab package dependencies

                  Lou Pecora wrote:
                  In article <mailman.654.11 61110013.11739. python-list@python.org >,
                  Robert Kern <robert.kern@gm ail.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.cf g 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_sc ripts�--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

                  Comment

                  • Lou Pecora

                    #10
                    Re: pylab package dependencies

                    In article <mailman.661.11 61121277.11739. python-list@python.org >,
                    Robert Kern <robert.kern@gm ail.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_script s?--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.

                    Comment

                    • amitsoni.1984@gmail.com

                      #11
                      Re: Plotting histograms

                      Thanks Robert,

                      My previous problem is solved(I was using 'from matplotlib.pyla b 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_nor mal((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:\Documen ts and Settings\amitso ni\Desktop\New
                      Folder\wignerpy thon", 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:
                      amitsoni.1984@g mail.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.pyla b 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

                      Comment

                      • Robert Kern

                        #12
                        Re: Plotting histograms

                        amitsoni.1984@g mail.com wrote:
                        Thanks Robert,
                        >
                        My previous problem is solved(I was using 'from matplotlib.pyla b 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_nor mal((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:\Documen ts and Settings\amitso ni\Desktop\New
                        Folder\wignerpy thon", 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

                        Comment

                        • Roberto Bonvallet

                          #13
                          Re: Plotting histograms

                          amitsoni.1984@g mail.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

                          Comment

                          • amitsoni.1984@gmail.com

                            #14
                            Re: Plotting histograms

                            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:
                            amitsoni.1984@g mail.com wrote:
                            Thanks Robert,

                            My previous problem is solved(I was using 'from matplotlib.pyla b 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_nor mal((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:\Documen ts and Settings\amitso ni\Desktop\New
                            Folder\wignerpy thon", 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

                            Comment

                            • Robert Kern

                              #15
                              Re: Plotting histograms

                              amitsoni.1984@g mail.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

                              Comment

                              Working...