C:\Users\Administrator\PycharmProjects\pythonProject2\.venv\Scripts\python.exe C:\Users\Administrator\PycharmProjects\pythonProject2\study.py
a. 子女身高分布形状分析:
偏度: 0.0580
峰度: -0.7953
Traceback (most recent call last):
File "C:\Users\Administrator\PycharmProjects\pythonProject2\study.py", line 54, in <module>
plt.savefig('/home/user/vibecoding/workspace/height_analysis/child_height_distribution.png', dpi=300, bbox_inches='tight')
File "C:\Users\Administrator\PycharmProjects\pythonProject2\.venv\lib\site-packages\matplotlib\pyplot.py", line 1250, in savefig
res = fig.savefig(*args, **kwargs) # type: ignore[func-returns-value]
File "C:\Users\Administrator\PycharmProjects\pythonProject2\.venv\lib\site-packages\matplotlib\figure.py", line 3490, in savefig
self.canvas.print_figure(fname, **kwargs)
File "C:\Users\Administrator\PycharmProjects\pythonProject2\.venv\lib\site-packages\matplotlib\backend_bases.py", line 2186, in print_figure
result = print_method(
File "C:\Users\Administrator\PycharmProjects\pythonProject2\.venv\lib\site-packages\matplotlib\backend_bases.py", line 2042, in <lambda>
print_method = functools.wraps(meth)(lambda *args, **kwargs: meth(
File "C:\Users\Administrator\PycharmProjects\pythonProject2\.venv\lib\site-packages\matplotlib\backends\backend_agg.py", line 481, in print_png
self._print_pil(filename_or_obj, "png", pil_kwargs, metadata)
File "C:\Users\Administrator\PycharmProjects\pythonProject2\.venv\lib\site-packages\matplotlib\backends\backend_agg.py", line 430, in _print_pil
mpl.image.imsave(
File "C:\Users\Administrator\PycharmProjects\pythonProject2\.venv\lib\site-packages\matplotlib\image.py", line 1657, in imsave
image.save(fname, **pil_kwargs)
File "C:\Users\Administrator\PycharmProjects\pythonProject2\.venv\lib\site-packages\PIL\Image.py", line 2566, in save
fp = builtins.open(filename, "w+b")
FileNotFoundError: [Errno 2] No such file or directory: '/home/user/vibecoding/workspace/height_analysis/child_height_distribution.png'
Process finished with exit code 1
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
import os
# 设置中文字体
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
# 创建数据
child_height = np.array([171, 174, 177, 178, 180, 181, 159, 169, 170, 170, 175, 175, 178, 173, 181, 164, 167, 168, 170, 170,
155, 161, 166, 170, 158, 160, 160, 162, 165, 168, 170, 153, 156, 158, 160, 162, 163, 165, 166, 170])
father_height = np.array([166, 171, 179, 174, 173, 170, 168, 168, 170, 170, 172, 175, 174, 170, 178, 175, 163, 168, 170, 172,
165, 182, 166, 178, 173, 170, 171, 167, 175, 172, 168, 163, 168, 174, 170, 170, 173, 172, 181, 180])
mother_height = np.array([158, 158, 168, 160, 162, 160, 153, 153, 167, 160, 160, 165, 160, 160, 165, 161, 166, 155, 160, 158,
157, 165, 156, 160, 160, 165, 150, 158, 160, 162, 163, 152, 155, 155, 162, 158, 160, 161, 158, 165])
# a. 分析子女身高分布形状(包括偏度和峰度)
print("a. 子女身高分布形状分析:")
# 计算偏度和峰度
def skewness(data):
mean = np.mean(data)
std = np.std(data)
return np.mean(((data - mean) / std) ** 3)
def kurtosis(data):
mean = np.mean(data)
std = np.std(data)
return np.mean(((data - mean) / std) ** 4) - 3
child_skewness = skewness(child_height)
child_kurtosis = kurtosis(child_height)
print(f"偏度: {child_skewness:.4f}")
print(f"峰度: {child_kurtosis:.4f}")
# 绘制子女身高直方图和Q-Q图
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
# 直方图
ax1.hist(child_height, bins=10, edgecolor='black', alpha=0.7)
ax1.set_title('子女身高分布直方图', fontsize=14)
ax1.set_xlabel('身高 (cm)')
ax1.set_ylabel('频数')
ax1.grid(True, alpha=0.3)
# Q-Q图
stats.probplot(child_height, plot=ax2)
ax2.set_title('子女身高Q-Q图', fontsize=14)
ax2.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig('/home/user/vibecoding/workspace/height_analysis/child_height_distribution.png', dpi=300, bbox_inches='tight')
plt.close()
# b. 绘制父亲身高、母亲身高的箱型图
fig, ax = plt.subplots(figsize=(10, 6))
# 准备数据
box_data = [father_height, mother_height]
box_labels = ['父亲身高', '母亲身高']
# 创建箱型图
box_plot = ax.boxplot(box_data, labels=box_labels, patch_artist=True)
# 设置箱线颜色
colors = ['darkgray', 'lightgray']
for patch, color in zip(box_plot['boxes'], colors):
patch.set_facecolor(color)
# 添加数据点
for i, data_series in enumerate(box_data):
# 为每个点添加随机水平偏移,避免重叠
x = np.random.normal(i+1, 0.04, size=len(data_series))
ax.scatter(x, data_series, alpha=0.6, s=30, edgecolor='black', linewidth=0.5)
ax.set_title('父亲身高与母亲身高箱型图对比', fontsize=14)
ax.set_ylabel('身高 (cm)', fontsize=12)
ax.set_xlabel('父母类型', fontsize=12)
ax.grid(True, alpha=0.3, axis='y')
# 添加均值点和标注
for i, data_series in enumerate(box_data):
mean_val = np.mean(data_series)
ax.scatter(i+1, mean_val, color='red', marker='*', s=200, zorder=5)
ax.annotate(f'均值: {mean_val:.1f}', (i+1, mean_val),
textcoords="offset points", xytext=(0,10), ha='center')
plt.tight_layout()
plt.savefig('/home/user/vibecoding/workspace/height_analysis/parent_height_boxplot.png', dpi=300, bbox_inches='tight')
plt.close()
# c. 绘制子女身高与父亲身高、母亲身高的散点图
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))
# 子女身高与父亲身高散点图
ax1.scatter(child_height, father_height, color='blue', marker='o', alpha=0.7, s=60, edgecolor='black', linewidth=0.5)
ax1.set_title('子女身高与父亲身高散点图', fontsize=14)
ax1.set_xlabel('子女身高 (cm)', fontsize=12)
ax1.set_ylabel('父亲身高 (cm)', fontsize=12)
ax1.grid(True, alpha=0.3)
# 添加趋势线
z1 = np.polyfit(child_height, father_height, 1)
p1 = np.poly1d(z1)
ax1.plot(child_height, p1(child_height), "r--", alpha=0.8, linewidth=2)
# 子女身高与母亲身高散点图
ax2.scatter(child_height, mother_height, color='red', marker='^', alpha=0.7, s=60, edgecolor='black', linewidth=0.5)
ax2.set_title('子女身高与母亲身高散点图', fontsize=14)
ax2.set_xlabel('子女身高 (cm)', fontsize=12)
ax2.set_ylabel('母亲身高 (cm)', fontsize=12)
ax2.grid(True, alpha=0.3)
# 添加趋势线
z2 = np.polyfit(child_height, mother_height, 1)
p2 = np.poly1d(z2)
ax2.plot(child_height, p2(child_height), "b--", alpha=0.8, linewidth=2)
plt.tight_layout()
plt.savefig('/home/user/vibecoding/workspace/height_analysis/height_scatterplots.png', dpi=300, bbox_inches='tight')
plt.close()
# d. 计算相关系数
print("\nd. 相关系数分析:")
def correlation_coefficient(x, y):
n = len(x)
mean_x = np.mean(x)
mean_y = np.mean(y)
numerator = np.sum((x - mean_x) * (y - mean_y))
denominator = np.sqrt(np.sum((x - mean_x)**2) * np.sum((y - mean_y)**2))
return numerator / denominator
corr_child_father = correlation_coefficient(child_height, father_height)
corr_child_mother = correlation_coefficient(child_height, mother_height)
print(f"子女身高与父亲身高相关系数: {corr_child_father:.4f}")
print(f"子女身高与母亲身高相关系数: {corr_child_mother:.4f}")
# e. 计算子女身高的描述性统计量
print("\ne. 子女身高描述性统计量:")
mean_child = np.mean(child_height)
median_child = np.median(child_height)
std_child = np.std(child_height)
min_child = np.min(child_height)
max_child = np.max(child_height)
q25_child = np.percentile(child_height, 25)
q75_child = np.percentile(child_height, 75)
print(f"均值: {mean_child:.4f}")
print(f"中位数: {median_child:.4f}")
print(f"标准差: {std_child:.4f}")
print(f"最小值: {min_child:.4f}")
print(f"最大值: {max_child:.4f}")
print(f"25%分位数: {q25_child:.4f}")
print(f"75%分位数: {q75_child:.4f}")
print(f"偏度: {child_skewness:.4f}")
print(f"峰度: {child_kurtosis:.4f}")
# 绘制描述性统计表格图
fig, ax = plt.subplots(figsize=(10, 6))
ax.axis('tight')
ax.axis('off')
# 准备统计数据
stats_data = [
['均值', f"{mean_child:.2f}"],
['中位数', f"{median_child:.2f}"],
['标准差', f"{std_child:.2f}"],
['最小值', f"{min_child:.2f}"],
['最大值', f"{max_child:.2f}"],
['25%分位数', f"{q25_child:.2f}"],
['75%分位数', f"{q75_child:.2f}"],
['偏度', f"{child_skewness:.4f}"],
['峰度', f"{child_kurtosis:.4f}"]
]
table = ax.table(cellText=stats_data, colLabels=['统计量', '数值'], cellLoc='center', loc='center', bbox=[0, 0, 1, 1])
table.auto_set_font_size(False)
table.set_fontsize(12)
table.scale(1, 2)
# 设置表格样式
for i in range(len(stats_data) + 1):
for j in range(2):
cell = table[(i, j)]
if i == 0: # 表头
cell.set_facecolor('#4CAF50')
cell.set_text_props(weight='bold', color='white')
else:
if i % 2 == 0:
cell.set_facecolor('#f0f0f0')
else:
cell.set_facecolor('white')
plt.title('子女身高描述性统计量', fontsize=16, pad=20)
plt.savefig('/home/user/vibecoding/workspace/height_analysis/child_height_statistics.png', dpi=300, bbox_inches='tight')
plt.close()
print("\n所有分析完成,图表已保存至工作目录")
如何解决,解决后的完整代码是什么
最新发布