望采纳,已帮你完成作业代码如下(包括完整注释),运行结果已截图至下方。
#首先,导入需要的模块
import random
import matplotlib.pyplot as plt
# 然后,生成年份数组
years = list(range(2022-50,2022))
# 接下来,生成年均气温和年均风速的随机数组
temps = [random.uniform(1,15) for _ in range(50)]
winds = [random.uniform(1,12) for _ in range(50)]
# 然后,将这些数据存入文件
with open('data_temp.txt','w') as f:
for year,temp,wind in zip(years,temps,winds):
f.write(f'{year},{temp},{wind}\n')
# 最后,读取文件中的数据,并使用 `matplotlib` 绘制年均气温与年份、年均风速与年份的变化图
temps,winds = [],[]
with open('data_temp.txt','r') as f:
for line in f:
year,temp,wind = map(float,line.strip().split(','))
temps.append(temp)
winds.append(wind)
fig,ax = plt.subplots(figsize=(10,6))
ax.plot(years,temps,label='Annual Average Temperature (°C)')
ax.plot(years,winds,label='Annual Average Wind Speed (m/s)')
ax.set_xlabel('Year')
ax.set_ylabel('Temperature / Wind Speed')
ax.set_title('Annual Average Temperature and Wind Speed')
ax.legend()
plt.show()
望采纳,已帮你完成作业代码如下(包括完整注释),运行结果已截图至下方。
#首先,导入需要的模块
import random
import matplotlib.pyplot as plt
# 然后,生成年份数组
years = list(range(2022-50,2022))
# 接下来,生成年均气温和年均风速的随机数组
temps = [random.uniform(1,15) for _ in range(50)]
winds = [random.uniform(1,12) for _ in range(50)]
# 然后,将这些数据存入文件
with open('data_temp.txt','w') as f:
for year,temp,wind in zip(years,temps,winds):
f.write(f'{year},{temp},{wind}\n')
# 最后,读取文件中的数据,并使用 `matplotlib` 绘制年均气温与年份、年均风速与年份的变化图
temps,winds = [],[]
with open('data_temp.txt','r') as f:
for line in f:
year,temp,wind = map(float,line.strip().split(','))
temps.append(temp)
winds.append(wind)
fig,ax = plt.subplots(figsize=(10,6))
ax.plot(years,temps,label='Annual Average Temperature (°C)')
ax.plot(years,winds,label='Annual Average Wind Speed (m/s)')
ax.set_xlabel('Year')
ax.set_ylabel('Temperature / Wind Speed')
ax.set_title('Annual Average Temperature and Wind Speed')
ax.legend()
plt.show()
```python
#首先,导入需要的模块
import random
import matplotlib.pyplot as plt
# 然后,生成年份数组
years = list(range(2022-50,2022))
# 接下来,生成年均气温和年均风速的随机数组
temps = [random.uniform(1,15) for _ in range(50)]
winds = [random.uniform(1,12) for _ in range(50)]
# 然后,将这些数据存入文件
with open('data_temp.txt','w') as f:
for year,temp,wind in zip(years,temps,winds):
f.write(f'{year},{temp},{wind}\n')
# 最后,读取文件中的数据,并使用 `matplotlib` 绘制年均气温与年份、年均风速与年份的变化图
temps,winds = [],[]
with open('data_temp.txt','r') as f:
for line in f:
year,temp,wind = map(float,line.strip().split(','))
temps.append(temp)
winds.append(wind)
fig,ax = plt.subplots(figsize=(10,6))
ax.plot(years,temps,label='Annual Average Temperature (°C)')
ax.plot(years,winds,label='Annual Average Wind Speed (m/s)')
ax.set_xlabel('Year')
ax.set_ylabel('Temperature / Wind Speed')
ax.set_title('Annual Average Temperature and Wind Speed')
ax.legend()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
# 生成50行3列的数组
arr = np.zeros((50, 3))
# 填充数组的第一列:年份
arr[:, 0] = 2022 - np.arange(50)
# 填充数组的第二列:年均气温
arr[:, 1] = np.random.randint(1, 16, 50)
# 填充数组的第三列:年均风速
arr[:, 2] = np.random.randint(1, 13, 50)
# 将数组存入文件
np.savetxt("data_temp.txt", arr, fmt="%d")
# 读取文件
arr = np.loadtxt("data_temp.txt")
# 绘制年均气温与年份的变化图
plt.plot(arr[:, 0], arr[:, 1], label="年均气温(℃)")
# 绘制年均风速与年份的变化图
plt.plot(arr[:, 0], arr[:, 2], label="年均风速(m/s)")
# 设置图形标题
plt.title("年均气温与年均风速随年份变化")
# 设置横坐标名称
plt.xlabel("年份")
# 设置纵坐标名称
plt.ylabel("年均气温(℃)/年均风速(m/s)")
# 设置图例
plt.legend()
# 显示图形
plt.show()