python数据可视化(三)字云

    下面将分两个章节具体学习wordCloud这个比较火的类。
    第一个章节学使用。
    第二个章节看源码,解释第一个章节中没有看懂的问题。

    Environment:eclipse+pydev python3

    • 1–分析使用方法:
      用到的库:WordCloud; matplotlib.pyplot
      (1)从txt文件中,读出要分析的文字数据;最好是用空格或者带标点的。这是由WordCloud的分析算法决定的,文件越大,程序越慢。
      (2)设定字图的风格,WordCloud给了很多可以设定的参数。会在后面逐个解说。
      (3)使用只有两句代码:
    wc1 = WordCloud(
        background_color="green",
        width=1000,
        height=860,
        font_path="C:/Windows/Fonts/simfang.ttf",margin=2)
    wc2 = wc1.generate(text)

    在WordCloud()的初始化函数中,设定参数
    在generate()函数中做字词分析,最后得到的是一个WordCloud()对象,把它画出来就行了。

    • 2–实现案例:
    #  coding: utf-8
    #  Description: to use matplotlib learn visuallized
    
    from wordcloud import WordCloud 
     # 数学绘图类
     # 解决:turnning interactive mode on的问题。
    import matplotlib.pyplot as plt
    
    
    def WordCloudExample():
        # preparetion: using the word from the file
        file =  open("D:\\Datas\\word.txt","r")
        text = file.read()
        #type =  chardet.detect(text)    
        #text1 = text.decode(type["encoding"])
    
    
    
        wc1 = WordCloud(
            background_color="green",
            width=1000,
            height=860,
            font_path="C:/Windows/Fonts/simfang.ttf",
            margin=2)
        wc2 = wc1.generate(text)
        #resultfile = open("D:\\Datas\\word1.txt","r")
        #resulttext = resultfile .read()
    
        plt.axis("off")
        plt.imshow(wc2)
        plt.show()
    
    • 0
      点赞
    • 1
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值