用matplotlib将中日儿童身高绘制成折线图
时间: 2023-12-17 14:58:14 浏览: 37
首先,我们需要准备好数据。假设我们有以下两个列表,其中第一个列表为中儿童身高,第二个列表为日儿童身高:
```
cn_height = [120, 130, 135, 140, 145, 150, 155, 160, 165, 170]
jp_height = [110, 120, 125, 130, 135, 140, 145, 150, 155, 160]
```
然后,我们可以使用Matplotlib来绘制折线图。以下是一份示例代码:
```python
import matplotlib.pyplot as plt
cn_height = [120, 130, 135, 140, 145, 150, 155, 160, 165, 170]
jp_height = [110, 120, 125, 130, 135, 140, 145, 150, 155, 160]
# 设置x轴和y轴的数据
x = range(1, len(cn_height) + 1)
y1 = cn_height
y2 = jp_height
# 绘制折线图
plt.plot(x, y1, label="中国儿童身高")
plt.plot(x, y2, label="日本儿童身高")
# 设置图例位置
plt.legend(loc="upper left")
# 设置横纵坐标轴的名称
plt.xlabel("年龄")
plt.ylabel("身高(cm)")
# 设置标题
plt.title("中日儿童身高对比")
# 显示图像
plt.show()
```
运行代码后,就可以得到一张中日儿童身高对比的折线图。