Plotting CDF of a pandas series in python

后端 未结 7 1156
情话喂你
情话喂你 2020-12-23 09:25

Is there a way to do this? I cannot seem an easy way to interface pandas series with plotting a CDF.

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-23 09:40

    I came here looking for a plot like this with bars and a CDF line:

    It can be achieved like this:

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    series = pd.Series(np.random.normal(size=10000))
    fig, ax = plt.subplots()
    ax2 = ax.twinx()
    n, bins, patches = ax.hist(series, bins=100, normed=False)
    n, bins, patches = ax2.hist(
        series, cumulative=1, histtype='step', bins=100, color='tab:orange')
    plt.savefig('test.png')
    

    If you want to remove the vertical line, then it's explained how to accomplish that here. Or you could just do:

    ax.set_xlim((ax.get_xlim()[0], series.max()))
    

    I also saw an elegant solution here on how to do it with seaborn.

提交回复
热议问题