Python制作疫情地图--第二弹 绘制词云
2020-04-03 本文已影响0人
Ahmed_Khpulwak
Python制作疫情地图
详细讲解视频地址——详细视频讲解
第二弹 绘制词云
导入所需模块
import openpyxl
from wordcloud import WordCloud
读取excel中的数据
# 读取数据
wb = openpyxl.load_workbook('data.xlsx')
# 获取工作表
ws = wb['国内义擎']
frequency_in = {}
for row in ws.values:
if row[0] == '省份':
pass
else:
frequency_in[row[0]] = float(row[1])
frequency_out = {}
sheet_name = wb.sheetnames
for each in sheet_name:
if "洲" in each:
ws = wb[each]
for row in ws.values:
if row[0] == '国家':
pass
else:
frequency_out[row[0]] = float(row[1])
生成词云图片
def generate_pic(frequency,name):
wordcloud = WordCloud(font_path="C:/Windows/Fonts/SIMLI.TTF",
background_color="white",
width=1920, height=1080)
# 根据确诊病例的数目生成词云
wordcloud.generate_from_frequencies(frequency)
# 保存词云
wordcloud.to_file('%s.png'%(name))
拓展:
如何使词云更加密集?
以下参数在上图代码中实例化Wordcloud类时填入!
- 如果max_words小,长尾不明显,词云中小字就少,很多地方没有词去填补空白,就显示得不密集。--> 增大max_words
- 如果max_font_size与min_font_size没拉开,低频词(长尾部分)就没有小到足够在高频词的空白中填入,字中空白就让整个云显得不密集。--> 增大最大和最小字体之间的差值。
- 最后调整relative_scaling使交叉方向的比例合适,一些词的空白处就能填入一些较短的低频词进一步减少空白。
调用函数
generate_pic(frequency_in,'国内义擎情况词云图')
generate_pic(frequency_out,'世界义擎词云图')
效果展示
全国各省份疫情词云世界疫情词云
附上完整代码
import openpyxl
from wordcloud import WordCloud
from pyecharts import options as opts
from pyecharts.charts import WordCloud
# 与即时显示图片相关的模块
'''
import matplotlib.pyplot as plt # 绘制图像的模块
import numpy as np
from PIL import Image
'''
# 读取数据
wb = openpyxl.load_workbook('data.xlsx')
sheet_names = wb.sheetnames
frequency_out = {}
for each in sheet_names:
if '洲' in each:
ws = wb[each]
for row in ws.values:
if row[1] == "累计确诊":
pass
else:
frequency_out[row[0]] = float(row[1])
else:
pass
# 以省份的确诊病例总数代表其出现的频率
frequency_in = {}
ws = wb['国内疫情']
for row in ws.values:
if row[1] == "累计确诊":
pass
else:
frequency_in[row[0]] = float(row[1])
def generate_pic(frequency,name):
# 这里可以事先准备一张图片,可以用作背景
# background_image = np.array(Image.open('pic.jpg'))
wordcloud = WordCloud(font_path="C:/Windows/Fonts/SIMLI.TTF",
background_color = "red",
# mask=background_image,
width=1920, height=1080
)
# 按照确诊病例数目生成词云
wordcloud.generate_from_frequencies(frequency)
wordcloud.to_file('%s.png'%name)
# 展示图片
# plt.imshow(wordcloud, interpolation="bilinear")
# plt.axis("off")
# plt.show()
# 调用函数
generate_pic(frequency_in,'国内疫情')
generate_pic(frequency_out,'国外疫情')