0


解决:pycharm绘制词云-中文会被显示成方框乱码

目录

一.问题1描述

   利用大数据进行文本分析,对文本进行处理后,希望直观的将数据绘制成图云查看分词效果,但整个词云全为方框乱码:

此时绘制词云的代码为:

# 绘制消极词云
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()

二. 问题1的原因及解决办法

      wordcloud默认是不支持显示中文字符的,中文会被显示成方框。可以尝试更改WordCloud的字体参数,以便正常显示中文字符。

代码示例:

# 绘制消极词云
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()

三.问题2描述

   修改后词云正常显示,但标题为方框乱码

四.问题2原因及解决办法

    同上,wordcloud默认是不支持显示中文字符的,中文会被显示成方框。但是标题需要另外设置。

代码展示

# 绘制消极词云
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()
 修改后的词云正常显示:![](https://img-blog.csdnimg.cn/b0e9779b8b9a4e9088c5691609fcc39b.png)

本文转载自: https://blog.csdn.net/weixin_50576361/article/details/130566834
版权归原作者 weixin_50576361 所有, 如有侵权,请联系我们删除。

“解决:pycharm绘制词云-中文会被显示成方框乱码”的评论:

还没有评论