美文网首页编程语言爱好者
Pyecharts词云图制作教程

Pyecharts词云图制作教程

作者: 罗罗攀 | 来源:发表于2021-04-22 14:06 被阅读0次

    前言

    之前我们使用wordcloud库制作了词云图,今天我们就来学习另外一种制作词云图的方法,那就是pyecharts库,与wordcloud库不同的是,pyecharts库除了可以制作词云图外,还可以制作30+ 种常见图表。

    我们可以通过pip来安装此库。

    pip install pyecharts
    

    需要注意的是,pyecharts库现在是有两个版本的,分为v0.5.X 和 v1 两个大版本,v0.5.X 和 v1 间不兼容,v1 是一个全新的版本,默认安装是v1版本,而且v,0.5.X已经不再更新了,所以本次教程默认使用v1版本的。其官方文档链接为(https://pyecharts.org/#/zh-cn/intro)。

    小试牛刀

    我们参考官方文档,就可以简单写出一个例子。

    from pyecharts.charts import WordCloud
    
    data = [('张三',67),('李四',43),('王五',20)]
    
    wc = WordCloud()
    wc.add(series_name="小试牛刀", data_pair=data)
    wc.render_notebook()
    

    实战

    这里需要注意的就是传入的数据格式为[(word1, count1), (word2, count2)]。所以实际案例中,我们就要构造成该格式的数据即可,还是以赘婿小说为例。

    from pyecharts.charts import WordCloud
    import jieba
    import collections
    
    fp = open('赘婿.txt','r')
    text = fp.read().strip().replace('\n','').replace(' ','')
    cut_words = list(jieba.cut(text))
    
    stopwords = [line.strip() for line in open('hit_stopwords.txt','r').readlines()]
    result = []
    for cut_word in cut_words:
        if cut_word not in stopwords:
            result.append(cut_word)
            
    data = collections.Counter(result)
    data = data.most_common(200)
    
    
    wc = WordCloud()
    wc.add(series_name="赘婿", data_pair=data)
    wc.render_notebook()
    

    最后,词云图的参数设置可以看https://pyecharts.org/#/zh-cn/basic_charts,今天的分享就到这了,我们下期再见~

    相关文章

      网友评论

        本文标题:Pyecharts词云图制作教程

        本文链接:https://www.haomeiwen.com/subject/jvmmlltx.html