How to plot cdf in matplotlib in Python?

后端 未结 5 1632
粉色の甜心
粉色の甜心 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: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

提交回复
热议问题