python 生成簡單的詞云

2021-08-20 11:50:02 字數 1884 閱讀 7020

frommatplotlibimportpyplot

fromwordcloudimportwordcloud,imagecolorgenerator

importjieba,pymysql

fromscipy.miscimportimread # 這是乙個處理影象的函式

conn = pymysql.connect(

host ='127.0.0.1',

port = 3306,

database ='learn',

user ='root',

password ='root',

charset ='utf8')

cursor = conn.cursor()

sql ="select name from beijing"cursor.execute(sql)

results = cursor.fetchall()

rets =''forretinresults:

# print (ret)

try:

rets += str(ret[0])

# print(rets)

except:

continue# 開始拼接字串

jieba.add_word('python爬蟲工程師')

jieba.add_word('python開發工程師')

jieba.add_word('python全棧工程師')

result = jieba.cut(rets, cut_all = false)

result ="/".join(result)

# 雲圖渲染

wc = wordcloud(background_color='white', # 背景顏色

max_words=1000, # 最大詞數

mask=back_color, # 以該引數值作圖繪製詞云,這個引數不為空時,width和height會被忽略

max_font_size=100, # 顯示字型的最大值

font_path="msyh.ttc", # 解決顯示口字型亂碼問題,可進入c:/windows/fonts/目錄更換字型

random_state=42, # 為每個詞返回乙個pil顏色

)wc.generate(result)

# 基於彩色影象生成相應彩色

image_colors = imagecolorgenerator(back_color)

# 數學渲染

pyplot.axis('off')# 關閉x,y軸

pyplot.imshow(wc) # 將雲圖新增到中渲染

pyplot.show()# 顯示

python生成詞云簡單案例

環境 python3.6 第三方庫 matplotlib wordcloud pil 如下 執行展示效果 分割線 以上使用於英文文字,中文文字流程如下 第三方庫 matplotlib wordcloud pil jieba numpy from wordcloud import wordcloud ...

Python 生成詞云

import matplotlib.pyplot as plt from wordcloud import wordcloud import jieba text from file with apath open python.txt encoding utf 8 read wordlist af...

python 生成詞云

coding utf 8 from wordcloud import wordcloud import matplotlib.pyplot as plt import jieba from pil import image import numpy as np 生成詞云 defcreate word...