注意要下载对应版本的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()