英文詞雲圖

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from wordcloud import WordCloud,ImageColorGenerator
%matplotlib inline
df_train = pd.read_csv('./testporject/train.csv')
train_qs = pd.Series(df_train['question1'].tolist() + df_train['question2'].tolist()).astype(str)
cloud = WordCloud(width=1440, height=1080).generate(" ".join(train_qs.astype(str)))
plt.figure(figsize=(20, 15))
plt.imshow(cloud)
plt.axis('off')

普通雲圖

下面以圖片為背景,展示圖片輪廓的雲圖

from PIL import Image
img = Image.open('./testporject/girl.png')
plt.imshow(img)
<matplotlib.image.AxesImage at 0x7fa9cc262ac8>

girl

img_array = np.array(img)
cloud_m = WordCloud(width=1440, height=1080,mask = img_array).generate(" ".join(train_qs.astype(str)))
plt.figure(figsize=(20, 15))
plt.imshow(cloud_m)
plt.axis('off')

girl雲圖

cloud_m.to_file('cloud.png')

下面是以圖片背景顏色為字型顏色實現

image_colors = ImageColorGenerator(img_array) # 讀取背景檔案色彩
plt.figure(figsize=(20, 15))
plt.imshow(cloud_m.recolor(color_func=image_colors))

colored雲圖

中文詞雲圖

用到的文字CalltoArms.txt是魯迅吶喊的節選。

import jieba
font = './testporject/FZSTK.TTF'
text = (open('./testporject/CalltoArms.txt','r',encoding='utf-8')).read()
cut = jieba.cut(text)  #分詞
string_ch = ' '.join(cut)
print(len(string_ch))
cloud_ch = WordCloud(width=1440, height=1080,
                     mask = img_array,
                    font_path=font).generate(string_ch)
plt.figure(figsize=(20, 15))
plt.imshow(cloud_ch)
plt.axis('off')

魯迅詞雲

本文中用到的相關資料和圖片連結: https://pan.baidu.com/s/1z-f8q6mIVTwK6AsERapWSw 提取碼: d42x