手把手教你python作词云,不断更新内容,有问题可以互相交流哦

不多废话,直接上代码:

#导入大致所需要的包
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)
#输出大致为:{'第一列第一个':第二列,'第一列第二个':第二列。。。}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

愣 头 汤

无偿分享,共同学习,打赏令惭愧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值