qqplot

hi all

has anyone ever tried to make a quantile-quantile plot with pylab?
is there any build in function named say "qqplot" available ?

thanks
Thierry

Thierry,

You need either scipy or rpy2 (and R) to do this. I've attached some code below. Please keep in mind that I've written for the general case of having a censored data set, therefore I rely on masked arrays from numpy.ma and scipy.stats.mstats -- but I have apply the mask midway through the process, which is different than the numpy's standard operating procedure. Let me know if any of this isn't clear.

I also have code that generates a quick comparison of the results from scipy.stats.mstats and ryp2+R, if you're interested.

HTH,
-paul

# code...
import matplotlib.pyplot as pl
import scipy.stats as st
import numpy as np

def censoredProbPlot(data, mask):
    ppos = st.mstats.plotting_positions(data)
    qntl = st.distributions.norm.ppf(ppos)
    
    qntlMask = np.ma.MaskedArray(qntl, mask=mask)
    dataMask = np.ma.MaskedArray(data, mask=mask)
    
    fit = st.mstats.linregress(dataMask, qntlMask)
    mu = -fit[1]
    sigma = fit[0]
    d_ = np.linspace(np.min(data),np.max(data))
    q_ = sigma * d_ - mu
    
    maskedProbPlot = {"mskData" : dataMask,
                      "mskQntl" : qntlMask,
                      "unmskData" : data,
                      "unmskQntl" : qntl,
                      "bestFitD" : d_,
                      "bestFitQ" : q_,
                      "mu" : mu,
                      "sigma" : sigma}
    return maskedProbPlot

if 1:
    #~~ you need to put your data here:
    #data = np.array([])
    #mask = np.array([],dtype=bool)

    mpp = censoredProbPlot(data, mask)

    fig = pl.figure()
    ax1 = fig.add_subplot(111)
    ax1.plot(mpp['mskQntl'], mpp['mskData'], 'ko', ms=6, label='Detected Samples')
    ax1.plot(mpp['unmskQntl'], mpp['unmskData'], 'r.', ms=6, label='Raw Samples')
    ax1.plot(mpp['bestFitQ'], mpp['bestFitD'], 'b-', lw=2)
    fig.savefig('example_censoredProbPlot.png')

···

-----Original Message-----
From: MONTAGU Thierry [mailto:thierry.montagu@…3117…]
Sent: Friday, May 21, 2010 6:37 AM
To: matplotlib-users@lists.sourceforge.net
Subject: [Matplotlib-users] qqplot

hi all

has anyone ever tried to make a quantile-quantile plot with pylab?
is there any build in function named say "qqplot" available ?

thanks
Thierry

-------------------------------------------------------------------------
-----

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

From: MONTAGU Thierry [mailto:thierry.montagu@…3117…]
Sent: Friday, May 21, 2010 09:37

has anyone ever tried to make a quantile-quantile plot with pylab?
is there any build in function named say "qqplot" available ?

For a plot comparing samples to a theoretical distribution (and if you don't
need masking as in Paul's example), you might be able to use
scipy.stats.probplot, as follows:

    import matplotlib.pyplot as plt
    import scipy.stats as st

    values = st.norm.rvs(size=(100,)) # example data
    fig = plt.figure() # set up plot
    ax = fig.add_subplot(1, 1, 1)
    osm, osr = st.probplot(values, fit=0, dist='norm') # compute
    ax.plot(osm, osr, '.') # plot

One way to include the fit line is

    (osm, osr), (m, b, r) = st.probplot(values, dist='norm') # compute
    osmf = osm.take([0, -1]) # endpoints
    osrf = m * osmf + b # fit line
    ax.plot(osm, osr, '.', osmf, osrf, '-')