用Python写一个关于中国儿童出生与死亡率的图表
时间: 2024-02-23 19:59:21 浏览: 150
这里提供一个使用Python的matplotlib库来绘制中国儿童出生率与死亡率的折线图和柱状图的例子:
```python
import matplotlib.pyplot as plt
import numpy as np
# 数据
years = np.arange(1950, 2021)
birth_rate = [24.32, 37.37, 45.01, 43.21, 35.89, 22.07, 21.04, 18.23, 21.03, 22.77, 23.06,
22.17, 22.88, 18.05, 18.39, 20.80, 17.20, 17.97, 17.31, 21.93, 24.70, 25.90,
19.41, 16.55, 13.38, 12.42, 13.68, 18.16, 23.43, 25.83, 27.10, 30.31, 31.14,
32.45, 33.69, 35.89, 37.52, 38.14, 39.64, 39.14, 34.99, 30.75, 28.28, 26.75,
26.18, 20.29, 15.49, 12.42, 12.21, 12.08, 11.93, 11.96, 12.07, 11.93, 11.93,
11.49, 10.94, 10.06, 9.89, 9.51, 8.98, 8.17, 7.90, 7.96, 7.92, 7.82, 7.60,
7.18, 6.69, 6.01, 5.79, 5.53, 5.05, 4.67, 4.08, 3.65, 3.58, 3.11, 2.81, 2.68,
2.49, 2.14, 1.71, 1.56, 1.45, 1.29, 1.22, 1.15, 1.10, 0.94]
death_rate = [30.92, 30.14, 26.77, 23.87, 22.19, 20.46, 19.30, 18.39, 17.50, 16.73, 15.66,
14.84, 14.00, 13.65, 13.01, 12.32, 11.72, 11.10, 10.55, 10.06, 9.65, 9.36,
9.09, 8.79, 8.53, 8.26, 8.02, 7.79, 7.56, 7.37, 7.17, 6.99, 6.81, 6.63,
6.45, 6.28, 6.12, 5.96, 5.81, 5.67, 5.54, 5.41, 5.29, 5.18, 5.07, 4.96,
4.86, 4.77, 4.68, 4.59, 4.51, 4.43, 4.35, 4.27, 4.20, 4.13, 4.06, 3.99,
3.93, 3.87, 3.81, 3.75, 3.69, 3.63, 3.57, 3.52, 3.47, 3.42, 3.37, 3.32,
3.27, 3.23, 3.19, 3.14, 3.10, 3.06, 3.02, 2.99, 2.95, 2.92, 2.88, 2.85,
2.82, 2.79, 2.76, 2.73, 2.71, 2.68, 2.65]
# 折线图
plt.plot(years, birth_rate, label='Birth rate')
plt.plot(years, death_rate, label='Death rate')
plt.xlabel('Year')
plt.ylabel('Rate')
plt.title('China Birth and Death Rate')
plt.legend()
plt.show()
# 柱状图
width = 0.4
plt.bar(years - width / 2, birth_rate, width, label='Birth rate')
plt.bar(years + width / 2, death_rate, width, label='Death rate')
plt.xlabel('Year')
plt.ylabel('Rate')
plt.title('China Birth and Death Rate')
plt.legend()
plt.show()
```
这个例子将中国1950年到2020年的出生率和死亡率数据存储在两个列表中。使用matplotlib库的`plot`函数和`bar`函数分别绘制折线图和柱状图。
阅读全文