词云图技术总结----大数据可视化

# -*- coding: utf-8 -*-

这行代码指定了Python源文件的编码格式为UTF-8.这样可以确保在读取和处理含中文字符的文件时不会出现编码问题。

import jieba
import matplotlib.pyplot as plt
import matplotlib.colors as colors
from wordcloud import WordCloud, STOPWORDS
from PIL import Image
import numpy as np

这段代码导入了所需的python库。jieba是用于中文分词库、matplotlib.pyplot、matplotlib.colors用于绘制图形和颜色的映射,WordCloud用于生成词云图。STOPWORDS是用于过滤无意义词的集合,PIL是Python Imaging Library用于打开和处理图片,numpy用于处理图像数据。

text = open("text.txt", encoding='utf-8').read()
text = text.replace('\n', "").replace("\u3000", "")
text_cut = jieba.lcut(text)
text_cut = ' '.join(text_cut)

这些代码用于读取包含文章文本的文件"text.txt",并将其存储在变量text中。同时,程序使用replace()函数删除了文章中的换行符和特殊空格符。然后,程序使用jieba库对文章进行分词处理,将分词结果保存在列表text_cut中。最后,程序将text_cut列表转换为一个字符串,以便后续处理。

stopwords = set()
content = [line.strip() for line in open('hit_stopwords.txt', 'r').readlines()]
stopwords.update(content)

这些代码用于读取包含无意义词的文件"hit_stopwords.txt",并将其保存在stopwords集合中。首先,程序创建了一个空集合stopwords。然后,程序使用for循环读取文件中的每一行,并使用strip()函数去掉行末的空格符。最后,程序使用update()函数将所有无意义词添加到stopwords集合中。

background = Image.open("dnn.jpg").convert('RGB')
graph = np.array(background)
colormaps = colors.ListedColormap(['#FF0000', '#FF7F50', '#FFE4C4'])
wordcloud = WordCloud(scale=4,
                      font_path="C:/Windows/Fonts/simhei.ttf",
                      background_color="white",
                      mask=graph,
                      colormap=colormaps,
                      relative_scaling=0.1,
                      stopwords=stopwords).generate(text_cut)

这些代码用于生成词云图。首先,程序使用PIL库打开名为"dnn.jpg"的图片,并将其转换为RGB格式。然后,程序使用numpy库将图片转换为数组形式,并将其保存在变量graph中。该图片将作为词云图的背景。接着,程序创建了一个名为wordcloud的词云对象,并设置了一些参数,包括:
scale: 词云图的缩放比例。font_path: 用于显示中文字符的字体文件路径。background_color: 词云图的

猜你喜欢

转载自blog.csdn.net/m0_56898461/article/details/130174057