How to plot confidence interval in Python?

前端 未结 2 1982
猫巷女王i
猫巷女王i 2020-12-09 20:51

I recently started to use Python and I can\'t understand how to plot a confidence interval for a given datum (or set of data). I already have a function that computes, given

2条回答
  •  时光说笑
    2020-12-09 21:33

    There are several ways to accomplish what you asking for:

    Using only matplotlib

    from matplotlib import pyplot as plt
    import numpy as np
    
    #some example data
    x= np.linspace(0.1, 9.9, 20)
    y = 3.0 * x
    #some confidence interval
    ci = 1.96 * np.std(y)/np.mean(y)
    
    fig, ax = plt.subplots()
    ax.plot(x,y)
    ax.fill_between(x, (y-ci), (y+ci), color='b', alpha=.1)
    

    fill_between does what you are looking for. For more information on how to use this function, see: https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.fill_between.html

    Output

    Alternatively, go for seaborn, which supports this using lineplot or regplot, see: https://seaborn.pydata.org/generated/seaborn.lineplot.html

提交回复
热议问题