Creating a Confidence Ellipses in a sccatterplot using matplotlib

前端 未结 2 1000
面向向阳花
面向向阳花 2020-12-04 11:32

How to creating a Confidence Ellipses in a sccatterplot using matplotlib?

The following code works until creating scatter plot. Then, does anyone familiar with putti

2条回答
  •  萌比男神i
    2020-12-04 12:07

    The following code draws a one, two, and three standard deviation sized ellipses:

    x = [5,7,11,15,16,17,18]
    y = [8, 5, 8, 9, 17, 18, 25]
    cov = np.cov(x, y)
    lambda_, v = np.linalg.eig(cov)
    lambda_ = np.sqrt(lambda_)
    from matplotlib.patches import Ellipse
    import matplotlib.pyplot as plt
    ax = plt.subplot(111, aspect='equal')
    for j in xrange(1, 4):
        ell = Ellipse(xy=(np.mean(x), np.mean(y)),
                      width=lambda_[0]*j*2, height=lambda_[1]*j*2,
                      angle=np.rad2deg(np.arccos(v[0, 0])))
        ell.set_facecolor('none')
        ax.add_artist(ell)
    plt.scatter(x, y)
    plt.show()
    

    enter image description here

提交回复
热议问题