python绘制年龄和身高折线图
时间: 2023-06-25 20:04:47 浏览: 321
下面是一个使用Python的matplotlib库绘制年龄和身高折线图的示例代码:
```python
import matplotlib.pyplot as plt
# 年龄和身高数据
ages = [18, 19, 20, 21, 22, 23, 24]
heights = [170, 172, 175, 178, 180, 182, 185]
# 绘制折线图
plt.plot(ages, heights)
# 设置图表标题和横纵坐标的标签
plt.title("Age and Height")
plt.xlabel("Age")
plt.ylabel("Height (cm)")
# 显示图表
plt.show()
```
运行以上代码,就可以得到一个简单的年龄和身高折线图。你可以根据自己的数据,修改ages和heights列表中的数据,来得到自己想要的折线图。
相关问题
绘制年龄和身高折线图 age CHeight JHeight 1 7 125 122 2 8 131 128 3 9 138 133 4 10 140 138 5 11 145 143 6 12 153 153 7 13 161 155 8 14 169 162 9 15 174 166 10 16 174 169 11 17 174 170 12 18 175 171
以下是使用 Python 绘制年龄和身高的折线图的代码:
```python
import matplotlib.pyplot as plt
# 数据
age = [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
c_height = [125, 131, 138, 140, 145, 153, 161, 169, 174, 174, 174, 175]
j_height = [122, 128, 133, 138, 143, 153, 155, 162, 166, 169, 170, 171]
# 绘图
plt.plot(age, c_height, label='CHeight')
plt.plot(age, j_height, label='JHeight')
plt.xlabel('Age')
plt.ylabel('Height')
plt.title('Age and Height')
plt.legend()
plt.show()
```
运行以上代码,可以得到年龄和身高的折线图:

用matplotlib绘制中日儿童身高的折线图
首先,需要准备好数据。假设我们有以下中日儿童身高数据:
```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()`函数显示图像。
阅读全文
相关推荐
















