绘制象限图并根据X和Y的平均值区分四组总体,通常用于数据分析和可视化。以下是详细步骤和相关概念:
以下是一个简单的Python示例,展示如何绘制象限图并根据X和Y的平均值区分四组总体:
import matplotlib.pyplot as plt
import numpy as np
# 生成示例数据
np.random.seed(0)
x = np.random.normal(0, 1, 100)
y = np.random.normal(0, 1, 100)
# 计算平均值
x_mean = np.mean(x)
y_mean = np.mean(y)
# 划分象限
quadrant1 = [(xi, yi) for xi, yi in zip(x, y) if xi > x_mean and yi > y_mean]
quadrant2 = [(xi, yi) for xi, yi in zip(x, y) if xi < x_mean and yi > y_mean]
quadrant3 = [(xi, yi) for xi, yi in zip(x, y) if xi < x_mean and yi < y_mean]
quadrant4 = [(xi, yi) for xi, yi in zip(x, y) if xi > x_mean and yi < y_mean]
# 绘制象限图
plt.figure(figsize=(8, 8))
plt.scatter(x, y, alpha=0.5)
plt.axvline(x=x_mean, color='r', linestyle='--')
plt.axhline(y=y_mean, color='r', linestyle='--')
plt.xlim(-3, 3)
plt.ylim(-3, 3)
plt.title('Quadrant Chart')
plt.xlabel('X')
plt.ylabel('Y')
# 标记象限
plt.text(x_mean + 0.5, y_mean + 0.5, 'Q1', fontsize=14)
plt.text(x_mean - 0.5, y_mean + 0.5, 'Q2', fontsize=14)
plt.text(x_mean - 0.5, y_mean - 0.5, 'Q3', fontsize=14)
plt.text(x_mean + 0.5, y_mean - 0.5, 'Q4', fontsize=14)
plt.show()
# 计算最终计数
print(f'Quadrant 1 count: {len(quadrant1)}')
print(f'Quadrant 2 count: {len(quadrant2)}')
print(f'Quadrant 3 count: {len(quadrant3)}')
print(f'Quadrant 4 count: {len(quadrant4)}')
通过以上步骤和示例代码,你可以绘制出象限图并根据X和Y的平均值区分四组总体,同时计算每组的最终计数。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云