编辑代码

import random
import csv
import datetime
from collections import Counter

# 模拟数据生成函数
def generate_sample_data(n=100):
    """生成模拟的社团报名数据"""
    # 基础数据列表
    names = ["张三", "李四", "王五", "赵六", "钱七", "孙八", "周九", "吴十", "郑十一", "王十二"]
    genders = ["男", "女"]
    colleges = ["计算机学院", "商学院", "文学院", "理学院", "工学院", "艺术学院", "法学院"]
    activities = ["编程社", "街舞社", "文学社", "摄影社", "音乐社", "辩论社"]
    
    data = []
    # 生成当前时间作为基准
    start_date = datetime.datetime(2025, 6, 1)
    end_date = datetime.datetime(2025, 6, 15)
    
    for i in range(1, n+1):
        # 随机生成数据
        name = random.choice(names) + f"{i}"
        gender = random.choice(genders)
        college = random.choice(colleges)
        activity = random.choice(activities)
        
        # 生成随机报名时间
        random_days = random.randint(0, (end_date - start_date).days)
        random_time = start_date + datetime.timedelta(days=random_days)
        sign_up_time = random_time.strftime("%Y-%m-%d %H:%M:%S")
        
        data.append([name, gender, college, activity, sign_up_time])
    
    return data

# 保存数据到CSV文件
def save_to_csv(data, filename="club_signup_data.csv"):
    """将数据保存到CSV文件"""
    with open(filename, 'w', newline='', encoding='utf-8') as f:
        writer = csv.writer(f)
        writer.writerow(["姓名", "性别", "学院", "报名社团", "报名时间"])
        writer.writerows(data)
    print(f"数据已保存到 {filename}")

# 数据统计分析函数
def analyze_data(data):
    """分析报名数据并返回统计结果"""
    results = {}
    
    # 1. 按性别统计报名人数
    genders = [row[1] for row in data]
    gender_count = dict(Counter(genders))
    results["性别分布"] = gender_count
    
    # 2. 按学院统计报名人数
    colleges = [row[2] for row in data]
    college_count = dict(Counter(colleges))
    results["学院分布"] = college_count
    
    # 3. 按社团统计报名人数
    activities = [row[3] for row in data]
    activity_count = dict(Counter(activities))
    results["社团报名人数"] = activity_count
    
    # 4. 按日期统计报名人数
    dates = [row[4].split()[0] for row in data]
    date_count = dict(Counter(dates))
    results["每日报名人数"] = date_count
    
    return results

# 数据可视化函数
def visualize_results(results):
    """可视化统计结果"""
    # 设置中文字体
    font = FontProperties(fname="/System/Library/Fonts/PingFang.ttc")  # macOS系统字体
    # 如果是Windows系统,可使用:font = FontProperties(fname="C:/Windows/Fonts/simhei.ttf")
    
    plt.figure(figsize=(18, 12))
    
    # 1. 性别分布饼图
    plt.subplot(2, 2, 1)
    labels, sizes = zip(*results["性别分布"].items())
    plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
    plt.axis('equal')  # 保证饼图是圆形
    plt.title("报名性别分布", fontproperties=font, fontsize=14)
    
    # 2. 学院分布柱状图
    plt.subplot(2, 2, 2)
    colleges, counts = zip(*results["学院分布"].items())
    plt.bar(colleges, counts, color='skyblue')
    plt.xticks(rotation=45, fontproperties=font)
    plt.title("各学院报名人数", fontproperties=font, fontsize=14)
    plt.xlabel("学院", fontproperties=font)
    plt.ylabel("报名人数", fontproperties=font)
    
    # 3. 社团报名人数柱状图
    plt.subplot(2, 2, 3)
    activities, counts = zip(*results["社团报名人数"].items())
    plt.bar(activities, counts, color='lightgreen')
    plt.xticks(rotation=45, fontproperties=font)
    plt.title("各社团报名人数", fontproperties=font, fontsize=14)
    plt.xlabel("社团名称", fontproperties=font)
    plt.ylabel("报名人数", fontproperties=font)
    
    # 4. 每日报名人数折线图
    plt.subplot(2, 2, 4)
    dates, counts = zip(*sorted(results["每日报名人数"].items()))
    plt.plot(dates, counts, marker='o', color='red')
    plt.xticks(rotation=45, fontproperties=font)
    plt.title("每日报名人数趋势", fontproperties=font, fontsize=14)
    plt.xlabel("日期", fontproperties=font)
    plt.ylabel("报名人数", fontproperties=font)
    
    plt.tight_layout()
    plt.savefig("signup_statistics.png")
    plt.show()

# 主函数
def main():
    # 生成100条模拟数据
    sample_data = generate_sample_data(100)
    
    # 保存数据到CSV
    save_to_csv(sample_data)
    
    # 分析数据
    results = analyze_data(sample_data)
    
    # 打印统计结果
    print("\n===== 社团招新报名数据统计 =====")
    for category, data in results.items():
        print(f"\n{category}:")
        for key, value in data.items():
            print(f"  {key}: {value}人")
    
    # 可视化结果
    visualize_results(results)

if __name__ == "__main__":
    main()