目錄
一.問題1描述
二. 問題1的原因及解決辦法
三.問題2描述
四.問題2原因及解決辦法
一.問題1描述
? ? ? ?利用大數(shù)據(jù)進行文本分析,對文本進行處理后,希望直觀的將數(shù)據(jù)繪制成圖云查看分詞效果,但整個詞云全為方框亂碼:
此時繪制詞云的代碼為:
# 繪制消極詞云
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默認是不支持顯示中文字符的,中文會被顯示成方框??梢試L試更改WordCloud的字體參數(shù),以便正常顯示中文字符。
代碼示例:
# 繪制消極詞云
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默認是不支持顯示中文字符的,中文會被顯示成方框。但是標題需要另外設置。
代碼展示:文章來源:http://www.zghlxwxcb.cn/news/detail-577467.html
# 繪制消極詞云
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()
? ? ?修改后的詞云正常顯示:文章來源地址http://www.zghlxwxcb.cn/news/detail-577467.html
到了這里,關于解決:pycharm繪制詞云-中文會被顯示成方框亂碼的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!