用matplotlib绘制中日儿童身高的折线图
时间: 2023-10-22 07:08:38 浏览: 126
首先,需要准备好数据。假设我们有以下中日儿童身高数据:
```python
# 中日儿童身高数据
ages = [2, 4, 6, 8, 10, 12, 14, 16]
heights_cn = [85, 105, 120, 135, 145, 155, 165, 170]
heights_jp = [88, 110, 125, 140, 150, 160, 170, 175]
```
接下来,我们可以使用matplotlib绘制折线图,代码如下:
```python
import matplotlib.pyplot as plt
# 中日儿童身高数据
ages = [2, 4, 6, 8, 10, 12, 14, 16]
heights_cn = [85, 105, 120, 135, 145, 155, 165, 170]
heights_jp = [88, 110, 125, 140, 150, 160, 170, 175]
# 绘制折线图
plt.plot(ages, heights_cn, label='中国儿童身高')
plt.plot(ages, heights_jp, label='日本儿童身高')
# 添加标题、坐标轴标签和图例
plt.title('中日儿童身高')
plt.xlabel('年龄')
plt.ylabel('身高(cm)')
plt.legend()
plt.show()
```
运行上述代码,即可得到中日儿童身高的折线图。其中,`plt.plot()`函数用于绘制折线图,`label`参数用于添加图例,`plt.title()`、`plt.xlabel()`和`plt.ylabel()`函数用于添加标题和坐标轴标签,`plt.legend()`函数用于显示图例。最后,使用`plt.show()`函数显示图像。