fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. }
  14. }
Success #stdin #stdout 0.08s 52624KB
stdin
import matplotlib.pyplot as plt
import matplotlib

# 设置中文正常显示
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False

# ========== 图1:每日睡眠时长分布 饼图 ==========
plt.figure(figsize=(8, 6))
labels = ['7小时及以上', '6-7小时', '不足6小时']
sizes = [37.2, 41.2, 21.6]
colors = ['#66b3ff', '#99ff99', '#ff9999']
explode = (0.02, 0.02, 0.05)

plt.pie(sizes, explode=explode, labels=labels, colors=colors,
        autopct='%1.1f%%', shadow=False, startangle=90)
plt.title('大学生每日睡眠时长分布(n=301)', fontsize=14)
plt.axis('equal')
plt.savefig('睡眠时长分布饼图.png', dpi=300, bbox_inches='tight')
plt.close()

# ========== 图2:主要睡眠问题占比 柱状图 ==========
plt.figure(figsize=(8, 6))
problems = ['入睡困难', '夜间易醒']
rates = [47.5, 32.9]
bars = plt.bar(problems, rates, color=['#4c72b0', '#55a868'], width=0.5)

for bar in bars:
    height = bar.get_height()
    plt.text(bar.get_x() + bar.get_width()/2., height,
             f'{height}%', ha='center', va='bottom', fontsize=11)

plt.title('大学生主要睡眠问题占比(n=301)', fontsize=14)
plt.ylabel('占比(%)', fontsize=12)
plt.ylim(0, 55)
plt.savefig('睡眠问题占比柱状图.png', dpi=300, bbox_inches='tight')
plt.close()

# ========== 图3:熬夜主要原因 柱状图 ==========
plt.figure(figsize=(9, 6))
reasons = ['刷短视频/社交平台', '完成学业任务', '其他原因']
reason_rates = [48.5, 32.2, 19.3]
bars = plt.bar(reasons, reason_rates, color=['#c44e52', '#dd8452', '#8172b3'], width=0.6)

for bar in bars:
    height = bar.get_height()
    plt.text(bar.get_x() + bar.get_width()/2., height,
             f'{height}%', ha='center', va='bottom', fontsize=11)

plt.title('大学生熬夜主要原因分布(n=301)', fontsize=14)
plt.ylabel('占比(%)', fontsize=12)
plt.ylim(0, 55)
plt.savefig('熬夜原因分布柱状图.png', dpi=300, bbox_inches='tight')
plt.close()

# ========== 图4:睡眠质量与焦虑情绪对比 柱状图 ==========
plt.figure(figsize=(8, 6))
groups = ['睡眠质量差学生', '睡眠良好学生']
anxiety_rates = [67.2, 21.4]
bars = plt.bar(groups, anxiety_rates, color=['#e15759', '#76b7b2'], width=0.5)

for bar in bars:
    height = bar.get_height()
    plt.text(bar.get_x() + bar.get_width()/2., height,
             f'{height}%', ha='center', va='bottom', fontsize=11)

plt.title('不同睡眠群体焦虑情绪占比对比(n=301)', fontsize=14)
plt.ylabel('经常焦虑占比(%)', fontsize=12)
plt.ylim(0, 75)
plt.savefig('睡眠与焦虑对比图.png', dpi=300, bbox_inches='tight')
plt.close()

print("4张数据图已生成,保存在当前文件夹中")
stdout
Standard output is empty