import matplotlib.pyplot as plt
import numpy as np
# 交叉分析数据
age_groups = ['18岁以下', '18-30岁', '31-40岁', '40岁以上']
students = np.array([30, 35, 0, 0])
newcomers = np.array([0, 24, 0, 0])
white_collars = np.array([0, 6, 9, 0])
others = np.array([0, 0, 0, 4])
x = np.arange(len(age_groups))
width = 0.2 # 柱形宽度
plt.figure(figsize=(10, 6))
# 绘制柱状图
plt.bar(x - 1.5*width, students, width, label='学生')
plt.bar(x - 0.5*width, newcomers, width, label='职场新人')
plt.bar(x + 0.5*width, white_collars, width, label='职场白领')
plt.bar(x + 1.5*width, others, width, label='其他')
# 坐标轴与标题
plt.xticks(x, age_groups, fontsize=12)
plt.ylabel('人数', fontsize=12)
plt.title('不同年龄段的身份分布(交叉分析)', fontsize=14)
plt.legend()
plt.tight_layout()
plt.show()