Python實現(xiàn)繪制自定義形狀的詞云示例
前言
本文為分總結構,有特定需求的可以查閱前部分分結構的對應板塊,最后的總結不懂的可以在分板塊查閱解釋。分板塊分別有引用的庫、閱讀文本、分詞并設置停用詞、設置png掩膜、字體設置、生成詞云圖,感謝您點開這篇分享,祝順利。
一、引用的庫
from wordcloud import WordCloud, STOPWORDS import matplotlib.pyplot as plt import numpy as np import jieba.posseg as pseg from collections import Counter import PIL.Image as Image from matplotlib import colors
確保已安裝以上的庫,不然運行會報錯
#安裝庫可以用清華的鏡像網(wǎng)站(可能會更新,可以上官網(wǎng)查詢地址)
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
二、閱讀文本(讓python閱讀)
# 閱讀文本(這里yourfile.txt,根據(jù)文本所在具體位置進行設置)
text = open("yourfile.txt", encoding="utf-8").read()
words = pseg.cut(text)這里"yourfile.txt",根據(jù)文本所在具體位置進行設置
三、分詞并設置停用詞
# 按指定長度和詞性提取詞
report_words = []
for word, flag in words:
if (len(word) >= 2) and ('n' in flag): #這里設置統(tǒng)計的字數(shù)
report_words.append(word)
# 設置停用詞
stopwords = set(STOPWORDS)
stopwords.update(["的", "感謝", "我代表", "以上", "報告", "表示誠摯感謝","戰(zhàn)略"])
# 去除停用詞
report_words = [word for word in report_words if word not in stopwords]
# 統(tǒng)計高頻詞匯
result = Counter(report_words).most_common(200) #詞的個數(shù)
# 建立詞匯字典
content = dict(result)
#輸出詞頻統(tǒng)計結果
for i in range(50):
word,flag=result[i]
print("{0:<10}{1:>5}".format(word,flag))
len(word)設置的是詞長,想要提取兩個兩個字的就設置2,三個三個字的就設置3(以此類推)
result = Counter(report_words).most_common(200) 這里的200是指統(tǒng)計200個詞用于繪制,可以根據(jù)需求設置
四、設置png掩膜
#設置png掩膜(yourfile.png根據(jù)實際路徑進行替換)
background = Image.open("yourfile.png")
mask = np.array(background)yourfile.png根據(jù)實際路徑進行替換
如果輸出結構還是長方形(正方形),應該是png圖片“有問題”,可以嘗試以下的處理
1.用p圖軟件把圖片改成純黑色(可能別的也可以,我沒試過)
2.用以下代碼把白色背景改為透明
# 如果當前位深是32的話,可以不用寫轉(zhuǎn)RGBA模式的這一句,但是寫上也沒啥問題
# 從RGB(24位)模式轉(zhuǎn)成RGBA(32位)模式
img = Image.open("yourfile.png").convert('RGBA')
W, L = img.size
white_pixel = (0, 0, 0, 0) # 白色
for h in range(W):
for i in range(L):
if img.getpixel((h, i)) == white_pixel:
img.putpixel((h, i), (255, 255, 255, 0)) # 設置透明
img.save("yourfile_new.png") # 自己設置保存地址這里有兩個參數(shù)需要修改
yourfile.png根據(jù)實際路徑進行替換,yourfile_new.png(這是修改后圖片)根據(jù)實際路徑進行替換
五、字體設置
# 設置字體樣式路徑 font_path = r"C:\Windows\Fonts\STLITI.TTF" # 設置字體大小 max_font_size =200 min_font_size =10 # 建立顏色數(shù)組,可更改顏色 color_list = ['#FF274B'] # 調(diào)用顏色數(shù)組 colormap = colors.ListedColormap(color_list)
字體樣式:一般都在這個路徑,可以自己根據(jù)需求修改或者下載想要的字體
字體大?。鹤畲蠛妥钚「鶕?jù)需求更改
字體顏色:可以不要這行代碼(默認設置),也可以根據(jù)需求設置一種或多種顏色(我這里只設置了一種)
六、生成詞云圖
# 生成詞云
wordcloud = WordCloud(scale=4, #輸出清晰度
font_path=font_path, #輸出路徑
colormap=colormap, #字體顏色
width=1600, #輸出圖片寬度
height=900, #輸出圖片高度
background_color='white', #圖片背景顏色
stopwords=stopwords, #停用詞
mask=mask, #掩膜
max_font_size=max_font_size, #最大字體大小
min_font_size=min_font_size) #最小字體大小
wordcloud.generate_from_frequencies(content)
# 使用 matplotlib 顯示詞云
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()
# 保存詞云圖
wordcloud.to_file("wordcloud.png")如果前面參數(shù)都是按著我的來設置的話,這里直接復制粘貼就好
總結
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
import numpy as np
import jieba.posseg as pseg
from collections import Counter
import PIL.Image as Image
from matplotlib import colors
# 閱讀文本(這里yourfile.txt,根據(jù)文本所在具體位置進行設置)
text = open("yourfile.txt", encoding="utf-8").read()
words = pseg.cut(text)
# 按指定長度和詞性提取詞
report_words = []
for word, flag in words:
if (len(word) >= 2) and ('n' in flag): #這里設置統(tǒng)計的字數(shù)
report_words.append(word)
# 統(tǒng)計高頻詞匯
result = Counter(report_words).most_common(200) #詞的個數(shù)
# 建立詞匯字典
content = dict(result)
#輸出詞頻統(tǒng)計結果
for i in range(50):
word,flag=result[i]
print("{0:<10}{1:>5}".format(word,flag))
# 設置停用詞
stopwords = set(STOPWORDS)
stopwords.update(["的", "感謝", "我代表", "以上", "報告", "表示誠摯感謝","戰(zhàn)略"])
#設置png掩膜(yourfile.png根據(jù)實際路徑進行替換)
background = Image.open("yourfile.png").convert('RGB')
mask = np.array(background)
'''
# 如果當前位深是32的話,可以不用寫轉(zhuǎn)RGBA模式的這一句,但是寫上也沒啥問題
# 從RGB(24位)模式轉(zhuǎn)成RGBA(32位)模式
img = Image.open("yourfile.png").convert('RGBA')
W, L = img.size
white_pixel = (0, 0, 0, 0) # 白色
for h in range(W):
for i in range(L):
if img.getpixel((h, i)) == white_pixel:
img.putpixel((h, i), (255, 255, 255, 0)) # 設置透明
img.save("yourfile_new.png") # 自己設置保存地址
'''
# 設置字體樣式路徑
font_path = r"C:\Windows\Fonts\STLITI.TTF"
# 設置字體大小
max_font_size =200
min_font_size =10
# 建立顏色數(shù)組,可更改顏色
color_list = ['#FF274B']
# 調(diào)用顏色數(shù)組
colormap = colors.ListedColormap(color_list)
# 生成詞云
wordcloud = WordCloud(scale=4, #輸出清晰度
font_path=font_path, #輸出路徑
colormap=colormap, #字體顏色
width=1600, #輸出圖片寬度
height=900, #輸出圖片高度
background_color='white', #圖片背景顏色
stopwords=stopwords, #停用詞
mask=mask, #掩膜
max_font_size=max_font_size, #最大字體大小
min_font_size=min_font_size) #最小字體大小
wordcloud.generate_from_frequencies(content)
# 使用 matplotlib 顯示詞云
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()
# 保存詞云圖
wordcloud.to_file("wordcloud.png")生成示例

到此這篇關于Python實現(xiàn)繪制自定義形狀的詞云示例的文章就介紹到這了,更多相關Python繪制詞云內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python使用Selenium實現(xiàn)模擬登錄的示例代碼
Selenium(本文基于python3.8)是一個功能強大的自動化測試工具,它可以用于模擬用戶在瀏覽器中的行為,比如點擊、輸入、滾動等等,本教程將詳細介紹如何使用Python編寫一個模擬登錄地爬蟲,使用XPath等多種元素匹配方法,需要的朋友可以參考下2023-08-08
python實現(xiàn)scrapy爬蟲每天定時抓取數(shù)據(jù)的示例代碼
這篇文章主要介紹了python實現(xiàn)scrapy爬蟲每天定時抓取數(shù)據(jù)的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
Pytorch搭建YoloV5目標檢測平臺實現(xiàn)過程
這篇文章主要為大家介紹了Pytorch搭建YoloV5目標檢測平臺實現(xiàn)過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04

