python代码生成词云图

注意要下载对应版本的pyecharts==0.5.11

# pip uninstall pyecharts
pip install pyecharts==0.5.11

方法一:

import pandas as pd
import jieba
from collections import Counter
from pyecharts import WordCloud
import random


# 函数接口
def simple_word_cloud(mylist, width, height, size_range, shape, target_html):
	cloud_data = [word for word in mylist]
	count = Counter(cloud_data)
	wordcloud = WordCloud(width=1300, height=620)
	wordcloud.add('', count.keys(), count.values(), word_size_range=[10, 100], shape='circle', rotate_step=30)
	wordcloud.render(target_html)

# 模拟读取csv中的某一列数据
# 生成:这里以生成dataframe为例
lists = ["大数据技术","HTML5","JQuery","JavaScript","css","MapReduce","Storm","AI","LSP","NLP","AlphaGo","Hadoop","Spark","Hive","NoSQL","Pig",'大数据','数据科学','python','人工智能',"Java","c","c++","c#","Go","css"]   
for i in range(random.randint(100,1000)): lists.append(random.choice(lists)) 
my_dict = {'测试用例':lists}
df = pd.DataFrame(my_dict)

# 读取:直接输入list集合
my_list = list(df['测试用例'])

# 参数传递
width=1200
height=600
size_range=[10,100]
shape = 'circle'
target_html = "./wordcloud.html" #
simple_word_cloud(my_list,width,height,size_range,shape,target_html)

效果
在这里插入图片描述
方法二:

def getWordCloud(filters):
    wc = wordcloud.WordCloud(font_path = "simkai.ttf",#指定字体类型
                            background_color = "white",#指定背景颜色
                            max_words = 200,  # 词云显示的最大词数
                            max_font_size = 200)#指定最大字号
    #                         mask = mask) #指定模板,可以是图片
    word_clean=" ".join(filters)
    wc = wc.generate(word_clean)##生成词云
    plt.imshow(wc)
    plt.axis("off")
    plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值