代码之家  ›  专栏  ›  技术社区  ›  SaadH

通过读取文件matplotlib中的值来绘制CDF

  •  1
  • SaadH  · 技术社区  · 6 年前

    我需要绘制从文件读取的整数值的cdf。我在学习这个榜样 here

    import numpy as np
    from pylab import *
    
    with open ("D:/input_file.txt", "r+") as f:
        data = f.readlines()
        X = [int(line.strip()) for line in data]
        Y  = exp([-x**2 for x in X])  # is this correct? 
    
        # Normalize the data to a proper PDF
        Y /= ... # not sure what to write here
    
        # Compute the CDF
        CY = ... # not sure what to write here
    
        # Plot both
        plot(X,Y)
        plot(X,CY,'r--')
    
        show()
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   msi_gerva    6 年前

    import numpy as np
    # -----------------
    data = [88,93,184,91,107,170,88,107,167,90];
    # -----------------
    # get PDF:
    ydata,xdata = np.histogram(data,bins=np.size(data),normed=True);
    # ----------------
    # get CDF:
    cdf = np.cumsum(ydata*np.diff(xdata));
    # -----------------
    print 'Sum:',np.sum(ydata*np.diff(xdata))