数据来源:爬虫boss详见,https://blog.csdn.net/cynthia_file/article/details/109598995
#获取数据
import pandas as pd
df_b = pd.read_csv(r'beijingDataAnaly.csv')
df_b.info()
df_s = pd.read_csv(r'shanghaiDataAnaly.csv')
df_s.info()
df = df_b.append(df_s)
df = df.drop_duplicates().fillna('')
import re
import jieba
from wordcloud import WordCloud
# 使用本地字体
font_path = 'fonts/SourceHanSerifK-Light.otf'
wc = WordCloud(font_path=font_path, background_color="white", max_words=1000,
max_font_size=100, random_state=42, width=800, height=600, margin=2)
# 加载停用词
def load_stopwords(file_path):
with open(file_path, 'r',encoding = 'utf-8') as f:
stopwords = [line.strip('\n') for line in f.readlines()]
return stopwords
stopwords = load_stopwords('stopwords.txt')
import imageio
import numpy as np
back_color = np.array(imageio.imread('wa.jpg'))
def random_color_func(word=None, font_size=None, position=None, orientation=None, font_path=None, random_state=None):
h = np.random.randint(90,360)
s = int(100.0 * 255.0 / 255.0)
l = int(100.0 * float(np.random.randint(60, 120)) / 255.0)
return "hsl({}, {}%, {}%)".format(h, s, l)
wc_s = WordCloud(font_path=font_path,
background_color='white',#背景色为白色
mask=back_color,#用上面传入的图片绘制词云
random_state=20,#设置有多少生成状态,即多少种词的颜色
color_func=random_color_func,#上面的颜色控制函数
)
#统计词频
word_dict = {}
for title in df['details']:
title = re.sub(r"[^\u4e00-\u9fff]", "", title) # 去除中文字符
seg_list = list(jieba.cut(title, cut_all=False)) # 分词
for word in seg_list:
# 如果分词后的词语只有一个词语,或者在停用词表中,则跳过
if len(word) < 2 or word in stopwords:
continue
if word not in word_dict:
word_dict[word] = 0
word_dict[word] += 1
wc_s.generate_from_frequencies(word_dict)
#画图
from matplotlib import pyplot as plt
%matplotlib inline
plt.figure(figsize=(8, 6))
plt.imshow(wc_s)#展示图片
plt.axis('off')#取消坐标轴
plt.show()
原图:
词云:
使用的文件:
字体及停用词链接:https://pan.baidu.com/s/1OnCFJVqfASfwsu49S0Oq2g
提取码:3min