绘制年龄和身高折线图 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
时间: 2023-06-25 13:04:15 浏览: 279
以下是使用 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()
```
运行以上代码,可以得到年龄和身高的折线图:
![age_height_plot](https://img-blog.csdn.net/20180730154028700?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3JlZ3VsYXJfYXJjaGl0ZWN0dXJl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/80)
相关问题
import pandas as pd import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['font.serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = False df = pd.read_csv("avgHgt.csv") x_values = range(7,19) plt.xticks(range(7,19)) plt.plot(x_values, df['CHeight'],label='A国男孩身高') plt.plot(x_values, df['JHeight'],label='B国男孩身高') plt.title('AB两国7-12岁男孩身高图') plt.xlabel('年龄/岁') plt.ylabel('身高/厘米') plt.legend() plt.show()请解释每行代码
```
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文字体为黑体
mpl.rcParams['font.serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False # 解决负号显示问题
df = pd.read_csv("avgHgt.csv") # 读取csv文件,生成DataFrame
x_values = range(7,19) # 生成x轴刻度值
plt.xticks(range(7,19)) # 设置x轴刻度值
plt.plot(x_values, df['CHeight'],label='A国男孩身高') # 绘制A国男孩身高折线图
plt.plot(x_values, df['JHeight'],label='B国男孩身高') # 绘制B国男孩身高折线图
plt.title('AB两国7-12岁男孩身高图') # 设置图表标题
plt.xlabel('年龄/岁') # 设置x轴标签
plt.ylabel('身高/厘米') # 设置y轴标签
plt.legend() # 显示图例
plt.show() # 展示图表
```
以上是代码的解释,代码的功能是读取一个csv文件,生成A国和B国7到12岁男孩身高的折线图,并添加相应的标签和标题。其中matplotlib库用于绘制图形,pandas库用于读取csv文件,mpl.rcParams用于设置图形属性。
下面这段代码,在微信小程序中存在哪些问题,请分析指出,并把修复后的代码发给我。addPic: function () { const _ = this; const { quality } = this.state Taro.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: ['album'], success(result) { const tempFilePath = result.tempFilePaths[0]; let size = result.tempFiles[0].size; if (size > 1024 * 1024 * 8) {//大于8M返回 return Taro.showToast({ title: '图片大于8M啦', icon: 'none' }) } if (size < 1024 * 800) { //小于800kb压缩 const status = await _.imgSecCheck(tempFilePath) if (status == true) { console.log('通过了处理'); //走检测通过处理 } } else { Taro.getImageInfo({ src: tempFilePath, success: function (res) { _.setState({//设置原始宽高 cWidth: res.width, cHeight: res.height }, async () => { try { const imagePath = await _.getCanvasImg(tempFilePath, res.width, res.height, quality); const status = await _.imgSecCheck(imagePath) if (status == true) { console.log('通过了处理'); //走检测通过处理 } } catch (error) { } }) } }) } } }) },
在微信小程序中,下面这段代码存在以下问题:
1. `const status = await _.imgSecCheck(tempFilePath)` 语句中没有使用 `async` 函数,无法使用 `await` 关键字,应该将该函数改为 `async` 函数。
2. `await` 关键字只能在 `async` 函数中使用,因此需要将 `success` 回调函数改为 `async` 函数。
3. `success` 回调函数中的 `_.setState` 函数在微信小程序中应该使用 `this.setData` 函数进行替换。
4. `Taro.getImageInfo` 函数在微信小程序中应该使用 `wx.getImageInfo` 函数进行替换。
下面是修复后的代码:
```
addPic: function () {
const _ = this;
const { quality } = this.state
Taro.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album'],
success: async function(result) {
const tempFilePath = result.tempFilePaths[0];
let size = result.tempFiles[0].size;
if (size > 1024 * 1024 * 8) {
//大于8M返回
return Taro.showToast({
title: '图片大于8M啦',
icon: 'none'
})
}
if (size < 1024 * 800) {
//小于800kb压缩
const status = await _.imgSecCheck(tempFilePath)
if (status == true) {
console.log('通过了处理');
//走检测通过处理
}
} else {
wx.getImageInfo({
src: tempFilePath,
success: async function(res) {
_.setData({
//设置原始宽高
cWidth: res.width,
cHeight: res.height
})
try {
const imagePath = await _.getCanvasImg(tempFilePath, res.width, res.height, quality);
const status = await _.imgSecCheck(imagePath)
if (status == true) {
console.log('通过了处理');
//走检测通过处理
}
} catch (error) {
}
}
})
}
}
})
},
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)