置信椭圆原理以及椭圆图形绘制

置信椭圆长短轴计算

在这里插入图片描述

    def confidence_oval(self,factor, ppf_rate):
        pca1_std = np.std(factor.iloc[:, 0])
        pca2_std = np.std(factor.iloc[:, 1])
        f_value = scipy.stats.f.ppf(ppf_rate, dfn=2, dfd=factor.iloc[:, 0].shape[0] - 2)
        x_axis = np.sqrt(
            pca1_std ** 2 * f_value * 2 * ((factor.iloc[:, 0].shape[0] - 1) / (factor.iloc[:, 0].shape[0] - 2)))
        y_axis = np.sqrt(
            pca2_std ** 2 * f_value * 2 * ((factor.iloc[:, 0].shape[0] - 1) / (factor.iloc[:, 0].shape[0] - 2)))
        x_axis = '%.2f' % x_axis
        y_axis = '%.2f' % y_axis

        return x_axis, y_axis

Python图形绘制

   def elli_plot(self,full_data, ellipse, y):
        '''

        :param full_data: pls后的点
        :param ellipse: [椭圆长轴,椭圆短轴]
        :param y:
        :return:
        '''
        fig = plt.figure(figsize=(15, 5))
        ax = fig.add_subplot(111)
        elli = Ellipse(xy=(0, 0), width=float(ellipse[0]) * 2, height=float(ellipse[1]) * 2)
        ax.add_patch(elli)
        # 偏厚
        outlier_data = y.loc[y[y.columns[0]] == 3, :]
 
        # 偏薄
        outlier_data_less = y.loc[y[y.columns[0]] == 1, :] 
        inner_data = full_data['pls']['pls'].loc[full_data['pls']['pls'].index.isin(outlier_data.index.tolist()+outlier_data_less.index.tolist()) == False, :]
        ax.plot(outlier_data.iloc[:, 0], outlier_data.iloc[:, 1], 'ro')
        ax.plot(outlier_data_less.iloc[:, 0], outlier_data_less.iloc[:, 1], 'bo')
        ax.plot(inner_data.iloc[:, 0], inner_data.iloc[:, 1], 'yo')
        name = str(self.picture_id)
        plt.savefig("E:\\shhl\\1118_两次PLS\\偏厚\\图\\"+name+".png")
        self.picture_id = self.picture_id +1
        plt.show()
from matplotlib.patches import Ellipse, Circle
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

ell1 = Ellipse(xy = (0.0, 0.0), width = 4, height = 8, angle = 30.0, facecolor= 'yellow', alpha=0.3)
cir1 = Circle(xy = (0.0, 0.0), radius=2, alpha=0.5)
ax.add_patch(ell1)
ax.add_patch(cir1)

x, y = 0, 0
ax.scatter([0,1], [0,1],color='red')
ax.scatter([2,1], [1,1],color='green')

plt.axis('scaled')

plt.axis('equal')   #changes limits of x or y axis so that equal increments of x and y have the same length

plt.show()
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值