Plotting histograms, scatter plots in Python

John Hunter jdhunter at ace.bsd.uchicago.edu
Fri Aug 6 16:08:19 EDT 2004


>>>>> "Colombes" == Colombes  <DrColombes at yahoo.com> writes:

    Colombes> What is the easiest way to generate some plots and
    Colombes> graphs in Python ?  Specifically interested in simple
    Colombes> histograms and scatter plots with circles and regression
    Colombes> lines.

Here's a little example of a histogram and regression plot using
matplotlib - looks easy enough to me!  Output image at
http://nitace.bsd.uchicago.edu:8080/files/share/demo.png

    from matplotlib.matlab import *

    x = randn(10000)  # some gaussian noise

    subplot(211)      # a subplot
    hist(x, 100)      # make a histogram
    grid(True)        # make an axes grid
    ylabel('histogram')

    # now do the regression...
    x = arange(0.0, 2.0, 0.05)
    y = 2+ 3*x + 0.2*randn(len(x))  # y is a linear function of x + nse

    # the bestfit line from polyfit
    m,b = polyfit(x,y,1)  # a line is 1st order polynomial...

    # plot the data with blue circles and the best fit with a thick
    # solid black line
    subplot(212)
    plot(x, y, 'bo', x, m*x+b, '-k', linewidth=2)
    ylabel('regression')
    grid(True)       

    # save the image to hardcopy
    savefig('demo')
    show()




More information about the Python-list mailing list