matplotlib人口金字塔
时间: 2023-07-12 18:06:23 浏览: 140
好的,以下是一个简单的使用matplotlib绘制人口金字塔的例子:
```python
import matplotlib.pyplot as plt
import numpy as np
# 模拟数据
age = np.arange(0, 101, 5)
male_count = np.array([200, 220, 300, 400, 500, 600, 700, 800, 900, 1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 50])
female_count = np.array([250, 270, 350, 450, 550, 650, 750, 850, 950, 1050, 950, 850, 750, 650, 550, 450, 350, 250, 150, 100])
total_count = male_count + female_count
fig, ax = plt.subplots()
# 绘制男性人口金字塔
ax.barh(age, male_count, height=3, color='blue', align='center', alpha=0.7)
# 绘制女性人口金字塔
ax.barh(age, -female_count, height=3, color='pink', align='center', alpha=0.7)
# 设置图形标题和轴标签
ax.set_title('Population Pyramid')
ax.set_xlabel('Number of People')
ax.set_ylabel('Age')
# 设置坐标轴范围和刻度
ax.set_xlim(-1200, 1200)
ax.set_xticks([-1000, -500, 0, 500, 1000])
ax.set_xticklabels([1000, 500, 0, 500, 1000])
ax.invert_yaxis()
# 绘制图例
ax.legend(['Male', 'Female'], loc='upper left')
plt.show()
```
运行上述代码,将生成一个人口金字塔图形,其中蓝色表示男性人口,粉色表示女性人口。图形横轴表示人口数量,纵轴表示年龄。您可以根据需要自定义数据和样式。
阅读全文
相关推荐















