Solution: pycharm draws word cloud - Chinese will be displayed as garbled boxes

Table of contents

1. Description of problem 1

2. The cause and solution of problem 1

3. Description of Question 2

4. Reasons and solutions for problem 2


1. Description of problem 1

       Using big data for text analysis, after processing the text, I hope to intuitively draw the data into a graph cloud to view the word segmentation effect, but the entire word cloud is full of garbled boxes:

At this time, the code for drawing the word cloud is:

# 绘制消极词云
negative_wordcloud_text = " ".join(negative_words)
#设置词云信息
negative_wordcloud = WordCloud(width=1200, height=800, background_color='white').generate(negative_wordcloud_text)
plt.figure(figsize=(12, 8))
plt.imshow(negative_wordcloud, interpolation="bilinear")
plt.axis("off")
plt.title("消极评论关键词")
plt.show()

2. The cause and solution of problem 1

          Wordcloud does not support displaying Chinese characters by default, and Chinese characters will be displayed as boxes. You can try to change the font parameters of WordCloud so that Chinese characters can be displayed normally.

Code example:

# 绘制消极词云
negative_wordcloud_text = " ".join(negative_words)
#--------------------------------------------此处修改------------------------
#设置词云信息
negative_wordcloud = WordCloud(font_path ="C:/Windows/Fonts/msyh.ttc",width=1200, height=800, background_color='white').generate(negative_wordcloud_text)
plt.figure(figsize=(12, 8))
plt.imshow(negative_wordcloud, interpolation="bilinear")
plt.axis("off")
plt.title("消极评论关键词")
plt.show()

3. Description of Question 2

       After the modification, the word cloud is displayed normally, but the title is garbled in the box

 4. Reasons and solutions for problem 2

        As above, wordcloud does not support displaying Chinese characters by default, and Chinese characters will be displayed as boxes. But the title needs to be set additionally.

Code display :

# 绘制消极词云
negative_wordcloud_text = " ".join(negative_words)
#--------------------------------------------此处修改------------------------
#设置词云信息
negative_wordcloud = WordCloud(font_path ="C:/Windows/Fonts/msyh.ttc",width=1200, height=800, background_color='white').generate(negative_wordcloud_text)
plt.figure(figsize=(12, 8))
plt.imshow(negative_wordcloud, interpolation="bilinear")
plt.axis("off")
# -----------------------------此处修改------------------------------------------
# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.title("消极评论关键词")
plt.show()

     The modified word cloud is displayed normally:

Guess you like

Origin blog.csdn.net/weixin_50576361/article/details/130566834