#plt.rcParams['font.family'] = 'SimHei'

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['font.family'] = 'SimHei'  # 设置中文字体

# 数据
national = [184, 207, 236, 262, 284, 311, 354, 437, 485, 550, 693, 762, 803, 896, 1070, 1331, 1746, 2336, 2641, 2834, 2972, 3180]
rural = [138, 158, 178, 199, 221, 246, 283, 347, 376, 417, 508, 553, 571, 621, 718, 855, 1118, 1434, 1768, 1876, 1895, 1973]
urban = [405, 434, 496, 562, 576, 603, 662, 802, 920, 1089, 1431, 1568, 1686, 1925, 2356, 3027, 3891, 4874, 5430, 5796, 6217, 6651]

# 画图
fig, axes = plt.subplots(3, 1, figsize=(7, 8))

# 画全国居民消费直方图，并获取 bins
n, bins, _ = axes[0].hist(national, bins=10, color='blue', alpha=0.7, edgecolor='black')
bin_centers = (bins[:-1] + bins[1:]) / 2  # 计算每个柱子的中心位置
axes[0].set_xticks(bin_centers)
axes[0].set_xticklabels([f"{int(x)}" for x in bin_centers], rotation=45)
axes[0].set_title("全国居民消费分布  ——  消费（元）", fontsize=12, pad=-10)

# 画农村居民消费直方图，并获取 bins
n, bins, _ = axes[1].hist(rural, bins=10, color='green', alpha=0.7, edgecolor='black')
bin_centers = (bins[:-1] + bins[1:]) / 2
axes[1].set_xticks(bin_centers)
axes[1].set_xticklabels([f"{int(x)}" for x in bin_centers], rotation=45)
axes[1].set_title("农村居民消费分布  ——  消费（元）", fontsize=12, pad=-10)

# 画城镇居民消费直方图，并获取 bins
n, bins, _ = axes[2].hist(urban, bins=10, color='red', alpha=0.7, edgecolor='black')
bin_centers = (bins[:-1] + bins[1:]) / 2
axes[2].set_xticks(bin_centers)
axes[2].set_xticklabels([f"{int(x)}" for x in bin_centers], rotation=45)
axes[2].set_title("城镇居民消费分布  ——  消费（元）", fontsize=12, pad=-10)

plt.tight_layout()
plt.show()

