目录

一.问题1描述

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

三.问题2描述

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

一.问题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()

     修改后的词云正常显示:

好文链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。
大家都在看: