How to plot cdf in matplotlib in Python?

后端 未结 5 1607
粉色の甜心
粉色の甜心 2020-12-14 08:51

I have a disordered list named d that looks like:

[0.0000, 123.9877,0.0000,9870.9876, ...]

I just simply want to plot a cdf gr

相关标签:
5条回答
  • 2020-12-14 09:19

    The numpy function to compute cumulative sums cumsum can be useful here

    In [1]: from numpy import cumsum
    In [2]: cumsum([.2, .2, .2, .2, .2])
    Out[2]: array([ 0.2,  0.4,  0.6,  0.8,  1. ])
    
    0 讨论(0)
  • 2020-12-14 09:21

    For an arbitrary collection of values, x:

    def cdf(x, plot=True, *args, **kwargs):
        x, y = sorted(x), np.arange(len(x)) / len(x)
        return plt.plot(x, y, *args, **kwargs) if plot else (x, y)
    

    ((If you're new to python, the *args, and **kwargs allow you to pass arguments and named arguments without declaring and managing them explicitly))

    0 讨论(0)
  • 2020-12-14 09:25

    As mentioned, cumsum from numpy works well. Make sure that your data is a proper PDF (ie. sums to one), otherwise the CDF won't end at unity as it should. Here is a minimal working example:

    import numpy as np
    from pylab import *
    
    # Create some test data
    dx = 0.01
    X  = np.arange(-2, 2, dx)
    Y  = exp(-X ** 2)
    
    # Normalize the data to a proper PDF
    Y /= (dx * Y).sum()
    
    # Compute the CDF
    CY = np.cumsum(Y * dx)
    
    # Plot both
    plot(X, Y)
    plot(X, CY, 'r--')
    
    show()
    

    enter image description here

    0 讨论(0)
  • 2020-12-14 09:28
    import matplotlib.pyplot as plt
    X=sorted(data)
    Y=[]
    l=len(X)
    Y.append(float(1)/l)
    for i in range(2,l+1):
        Y.append(float(1)/l+Y[i-2])
    plt.plot(X,Y,color=c,marker='o',label='xyz')
    

    I guess this would do,for the procedure refer http://www.youtube.com/watch?v=vcoCVVs0fRI

    0 讨论(0)
  • 2020-12-14 09:42

    I know I'm late to the party. But, there is a simpler way if you just want the cdf for your plot and not for future calculations:

    plt.hist(put_data_here, normed=True, cumulative=True, label='CDF',
             histtype='step', alpha=0.8, color='k')
    

    As an example,

    plt.hist(dataset, bins=bins, normed=True, cumulative=True, label='CDF DATA', 
             histtype='step', alpha=0.55, color='purple')
    # bins and (lognormal / normal) datasets are pre-defined
    

    EDIT: This example from the matplotlib docs may be more helpful.

    0 讨论(0)
提交回复
热议问题