教你用python 画词云

import matplotlib.pyplot as plt
import jieba
# from wordcloud import WordCloud
# f=open('text','r',encoding='utf8')
# text=f.read().replace("\n",'').replace("-",'').replace("        ",'')
# print(text)
# wordcloud = WordCloud().generate(text)
from wordcloud import WordCloud
import PIL.Image as image
import numpy as np
import jieba


# 分词
def trans_CN(text):
    # 接收分词的字符串
    word_list = jieba.cut(text)
    # 分词后在单独个体之间加上空格
    result = " ".join(word_list)
    return result


with open("text","r",encoding='utf8') as fp:
    text = fp.read()
    print(text)
    # 将读取的中文文档进行分词
    text = trans_CN(text)
    print(1)
    # mask = np.array(image.open("F:\wordcloud\image\love.jpg"))
    wordcloud = WordCloud(
        # 添加遮罩层
        # mask=mask,
        # 生成中文字的字体,必须要加,不然看不到中文
        font_path="C:\Anacoda\Lib\site-packages\wordcloud\STFANGSO.ttf"
    ).generate(text)

    image_produce = wordcloud.to_image()
    image_produce.show()

词云是挺有意思的一个库,但其默认不支持中文字体,一读中文就乱码 下面分享下从根本上解决乱码问题的方法。
解决中文词云乱码问题

2015574-1df55e1030be127c.png
image.png

猜你喜欢

转载自blog.csdn.net/weixin_33877885/article/details/87316570