当前位置:   article > 正文

python制作词云,解决中文乱码问题_wc=wordcloud(background_color='white',width=500,he

wc=wordcloud(background_color='white',width=500,height=350,max_font_size=60,

英文

import matplotlib.pyplot as plt
import jieba
from wordcloud import WordCloud
text = open(r'test.txt', "r",encoding="utf-8").read()
cut_text = jieba.cut(text)
result = " ".join(cut_text)

wc = WordCloud(
    background_color='white',
    width=500,
    height=350,
    max_font_size=50,
    min_font_size=10,
    mode='RGBA'
)
wc.generate(result)
wc.to_file(r"./wordcloud.png")
plt.imshow(wc)
plt.axis("off")
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

在这里插入图片描述
在这里插入图片描述

中文

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

乱码了怎么办?

加一个 font_path=“simhei.ttf”

import matplotlib.pyplot as plt
import jieba
from wordcloud import WordCloud
text = open(r'test.txt', "r",encoding="utf-8").read()
cut_text = jieba.cut(text)
result = " ".join(cut_text)

wc = WordCloud(
    font_path="simhei.ttf",  #在这里添加
    background_color='white',
    width=500,
    height=350,
    max_font_size=50,
    min_font_size=10,
    mode='RGBA'
)
wc.generate(result)
wc.to_file(r"./wordcloud.png")
plt.imshow(wc)
plt.axis("off")
plt.show()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

在这里插入图片描述

好了!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/420769
推荐阅读
相关标签
  

闽ICP备14008679号