import pandas as pd import numpy as np import matplotlib.pyplot as plt import jieba import requests import re from io import BytesIO import imageio # 设置城市和时间 city = '上海' year = 2021 quarter = 2 # 爬取数据 url = f'http://tianqi.2345.com/t/wea_history/js/{city}/{year}/{quarter}.js' response = requests.get(url) text = response.content.decode('gbk') # 正则表达式匹配 pattern = re.compile(r'(\d{4}-\d{2}-\d{2})\|(\d{1,2})\|(\d{1,2})\|(\d{1,3})\|(\d{1,3})\|(\D+)\n') result = pattern.findall(text) # 数据整理 data = pd.DataFrame(result, columns=['日期', '最高温度', '最低温度', '空气质量指数', '风力等级', '天气']) data[['最高温度', '最低温度', '空气质量指数', '风力等级']] = data[['最高温度', '最低温度', '空气质量指数', '风力等级']].astype(int) data['日期'] = pd.to_datetime(data['日期']) # 可视化分析 # 统计天气情况 weather_count = data['天气'].value_counts() weather_count = weather_count[:10] # 分词统计 seg_list = jieba.cut(' '.join(data['天气'].tolist())) words = {} for word in seg_list: if len(word) < 2: continue if word in words: words[word] += 1 else: words[word] = 1 # 绘制柱状图和词云图 plt.figure(figsize=(10, 5)) plt.bar(weather_count.index, weather_count.values) plt.title(f'{city}{year}年第{quarter}季度天气情况') plt.xlabel('天气') plt.ylabel('次数') plt.savefig('weather_bar.png') wordcloud = pd.DataFrame(list(words.items()), columns=['word', 'count']) mask_image = imageio.imread('cloud_mask.png') wordcloud.plot(kind='scatter', x='count', y='count', alpha=0.5, s=300, cmap='Reds', figsize=(10, 5)) for i in range(len(wordcloud)): plt.text(wordcloud.iloc[i]['count'], wordcloud.iloc[i]['count'], wordcloud.iloc[i]['word'], ha='center', va='center', fontproperties='SimHei') plt.axis('off') plt.imshow(mask_image, cmap=plt.cm.gray, interpolation='bilinear') plt.savefig('weather_wordcloud.png')这个python代码有错误,请改正以使该代码运行成功
时间: 2023-09-04 08:16:00 浏览: 120
这段代码的问题在于缺少了一些必要的包和文件,比如 cloud_mask.png,同时也需要安装 jieba 和 imageio 这两个包。以下是修改后的代码:
```
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import jieba
import requests
import re
from io import BytesIO
import imageio
# 设置城市和时间
city = '上海'
year = 2021
quarter = 2
# 爬取数据
url = f'http://tianqi.2345.com/t/wea_history/js/{city}/{year}/{quarter}.js'
response = requests.get(url)
text = response.content.decode('gbk')
# 正则表达式匹配
pattern = re.compile(r'(\d{4}-\d{2}-\d{2})\|(\d{1,2})\|(\d{1,2})\|(\d{1,3})\|(\d{1,3})\|(\D+)\n')
result = pattern.findall(text)
# 数据整理
data = pd.DataFrame(result, columns=['日期', '最高温度', '最低温度', '空气质量指数', '风力等级', '天气'])
data[['最高温度', '最低温度', '空气质量指数', '风力等级']] = data[['最高温度', '最低温度', '空气质量指数', '风力等级']].astype(int)
data['日期'] = pd.to_datetime(data['日期'])
# 可视化分析
# 统计天气情况
weather_count = data['天气'].value_counts()
weather_count = weather_count[:10]
# 分词统计
seg_list = jieba.cut(' '.join(data['天气'].tolist()))
words = {}
for word in seg_list:
if len(word) < 2:
continue
if word in words:
words[word] += 1
else:
words[word] = 1
# 绘制柱状图和词云图
plt.figure(figsize=(10, 5))
plt.bar(weather_count.index, weather_count.values)
plt.title(f'{city}{year}年第{quarter}季度天气情况')
plt.xlabel('天气')
plt.ylabel('次数')
plt.savefig('weather_bar.png')
wordcloud = pd.DataFrame(list(words.items()), columns=['word', 'count'])
mask_image = imageio.imread('cloud_mask.png')
plt.figure(figsize=(10, 5))
plt.imshow(mask_image, cmap=plt.cm.gray, interpolation='bilinear')
plt.axis('off')
plt.savefig('mask.png')
wordcloud.plot(kind='scatter', x='count', y='count', alpha=0.5, s=300, cmap='Reds', figsize=(10, 5))
for i in range(len(wordcloud)):
plt.text(wordcloud.iloc[i]['count'], wordcloud.iloc[i]['count'], wordcloud.iloc[i]['word'], ha='center', va='center', fontproperties='SimHei')
plt.axis('off')
plt.savefig('weather_wordcloud.png')
```
注意:这里的 cloud_mask.png 是一个用于生成词云图的模板图片,可以替换成自己喜欢的图片。
阅读全文