gpt4 book ai didi

python - 使用 matplotlib 在 sccatterplot 中创建置信椭圆

转载 作者:IT老高 更新时间:2023-10-28 21:07:03 24 4
gpt4 key购买 nike

如何使用 matplotlib 在 sccatterplot 中创建置信椭圆?

以下代码在创建散点图之前一直有效。那么,有谁熟悉推杆散点图上的置信椭圆?

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 的置信椭圆引用。

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

sas中的代码是这样的:

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;

最佳答案

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

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

关于python - 使用 matplotlib 在 sccatterplot 中创建置信椭圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20126061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com