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
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 = {}
genders = [row[1] for row in data]
gender_count = dict(Counter(genders))
results["性别分布"] = gender_count
colleges = [row[2] for row in data]
college_count = dict(Counter(colleges))
results["学院分布"] = college_count
activities = [row[3] for row in data]
activity_count = dict(Counter(activities))
results["社团报名人数"] = activity_count
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")
plt.figure(figsize=(18, 12))
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)
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)
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)
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():
sample_data = generate_sample_data(100)
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()