不多废话,直接上代码:
#导入大致所需要的包
from wordcloud import WordCloud, ImageColorGenerator
from PIL import Image
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
ddata= {"You're The One": 76548,
'Revelry': 52581,
'Undo': 50788,
'Horn Concerto No. 4 in E flat K495: II. Romance (Andante cantabile)': 39269,
'Canada': 39137,
'Dog Days Are Over (Radio Edit)': 36992,
'Sehr kosmisch': 35537,
'Invalid': 33487,
'Catch You Baby (Steve Pitron & Max Sanna Radio Edit)': 31187,
'Secrets': 30482,
'Sincerité Et Jalousie': 27441,
'Représente': 27230,
'The Gift': 26032,
'Hey_ Soul Sister': 22956,
"Ain't Misbehavin": 22617,
"I CAN'T GET STARTED": 22482,
'Fireflies': 20633,
'Use Somebody': 19878,
'Marry Me': 19211,
'Home': 18437}
from imageio import imread
import wordcloud
im=imread("D:\downloads\VCG211390388723.png",pilmode="CMYK")#读取图片;这里已经转化二进制
w = wordcloud.WordCloud(font_path="SIMLI.TTF",
mask=im,#背景使用im
# width=200,
# height=100,
# max_words=30,#设置显示词量
max_font_size=40,#显示最大字体
background_color='white')#底色为白色
w.generate_from_frequencies(ddata)#导入数据
plt.imshow(w)#绘制词云
效果图:
以上代码为对字典类型进行词云展现:
"You're The One": 76548,#前面是标题,后边的是频数
'Revelry': 52581,
'Undo': 50788,
'Horn Concerto No. 4 in E flat K495: II. Romance (Andante cantabile)': 39269,
'Canada': 39137,
'Dog Days Are Over (Radio Edit)': 36992,
'Sehr kosmisch': 35537,
也就是说这个已经被处理完后的数据,那么如何让处理直接开始的一篇文章呢,是需要jieba进行分词,jieba教程到处都有,那咱就直接进行已经分词过的部分吧。
#直接使用我喜欢的一首歌的歌词当数据吧
data = 'Hello darkness my old friend,Icome to talk with U again,\
Because a vision softly creeping,Left its seeds while I was sleeping\
And the vision that was planted in my brain,Still remains,\
Within the sound of silence,In restless dreams I walk alone,\
Narrow streets of cobble stone,\
Neath the halo of a street lamp,\
I turned my collar to the cold and damp,\
When my eyes were stabbed by the flash of a neon light,\
That split the night,\
And touched the sound of silence,\
And in the naked light I saw,\
Ten thousand people maybe more,\
People talking without speaking,\
People hearing without listening,\
People writing songs that voices never share,\
And no one dare'
w = wordcloud.WordCloud(font_path="SIMLI.TTF",
mask=im,#背景使用im
# width=200,
# height=100,
# max_words=30,#设置显示词量
max_font_size=50,#显示最大字体
background_color='white')#底色为白色
w.generate(data)#导入数据!!!这里是要改变的,这时候我们是对字符串类型进行可视化。
plt.imshow(w)#绘制词云
w.to_file('xi瓜大师.png')#保存图片
效果图:
相关:
使用透明背景的png能避免不出图案的情况
想要列表转字典类型可以使用zip函数:
#热门歌手词云
#d1=zip(第一列,第二列)
d1=zip(list(popular_artist_top_20['artist_name']),list(popular_artist_top_20['listen_count']))
dict(d1)
#输出大致为:{'第一列第一个':第二列,'第一列第二个':第二列。。。}