使用matplotlib在散点图中创建置信椭圆 [英] Creating a Confidence Ellipses in a sccatterplot using matplotlib

查看:1084
本文介绍了使用matplotlib在散点图中创建置信椭圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用matplotlib在散点图中创建置信椭圆?

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

以下代码在创建散点图之前一直有效.然后,有人熟悉推杆吗 散点图中的置信椭圆吗?

The following code works until creating scatter plot. Then, does anyone familiar with putting Confidence Ellipses over the scatter plot?

import numpy as np
import matplotlib.pyplot as plt
x = [5,7,11,15,16,17,18]
y = [8, 5, 8, 9, 17, 18, 25]

plt.scatter(x,y)
plt.show()

以下是SAS的置信椭圆的参考.

Following is the reference for Confidence Ellipses from SAS.

http://support.sas.com/documentation/cdl/zh-CN/grstatproc/62603/HTML/default/viewer.htm#a003160800.htm

sas中的代码如下:

The code in sas is like this:

proc sgscatter data=sashelp.iris(where=(species="Versicolor"));
  title "Versicolor Length and Width";
  compare y=(sepalwidth petalwidth)
          x=(sepallength petallength)
          / reg ellipse=(type=mean) spacing=4;
run;

推荐答案

以下代码绘制一个,两个和三个标准偏差大小的椭圆:

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()

这篇关于使用matplotlib在散点图中创建置信椭圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆