亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

python詞云可視化方法總結(jié)記錄【簡單詞云+背景圖片詞云+自定義字體顏色詞云】

系統(tǒng) 1782 0

? ? ? ?詞云是一種非常漂亮的可視化展示方式,正所謂一圖勝過千言萬語,詞云在之前的項目中我也有過很多的使用,可能對于我來說,一種很好的自我介紹方式就是詞云吧,就像下面這樣的:

python詞云可視化方法總結(jié)記錄【簡單詞云+背景圖片詞云+自定義字體顏色詞云】_第1張圖片

? ? ?個人覺還是會比枯燥的文字語言描述性的介紹會更吸引人一點(diǎn)吧。

? ? ? 今天不是說要怎么用詞云來做個人介紹,而是對工作中使用到比較多的詞云計較做了一下總結(jié),主要是包括三個方面:

1、諸如上面的簡單形式矩形詞云

2、基于背景圖片數(shù)據(jù)來構(gòu)建詞云數(shù)據(jù)

3、某些場景下不想使用類似上面的默認(rèn)的字體顏色,這里可以自定義詞云的字體顏色

? ? ? 接下來對上面三種類型的詞云可視化方法進(jìn)行demo實(shí)現(xiàn)與展示,具體如下:

? ? ? 這里我們使用到的測試數(shù)據(jù)如下:

            
              The Zen of Python, by Tim Peters
            Beautiful is better than ugly.
        Explicit is better than implicit.
        Simple is better than complex.
        Complex is better than complicated.
        Flat is better than nested.
        Sparse is better than dense.
        Readability counts.
        Special cases aren't special enough to break the rules.
        Although practicality beats purity.
        Errors should never pass silently.
        Unless explicitly silenced.
        In the face of ambiguity, refuse the temptation to guess.
        There should be one-- and preferably text one --obvious way to do it.
        Although that way may not be obvious at first unless you're Dutch.
        Now is better than never.
        Although never is often better than *right* now.
        If the implementation is hard to explain, it's a bad idea.
        If the implementation is easy to explain, it may be a good idea.
        Namespaces are one honking great idea -- let's do more of those!
            
          


1、簡單形式矩形詞云實(shí)現(xiàn)如下:

            
              def simpleWC1(sep=' ',back='black',freDictpath='data_fre.json',savepath='res.png'):
    '''
    詞云可視化Demo
    '''
    try:
        with open(freDictpath) as f:
            data=f.readlines()
            data_list=[one.strip().split(sep) for one in data if one]
        fre_dict={}
        for one_list in data_list:
            fre_dict[unicode(one_list[0])]=int(one_list[1])
    except:
        fre_dict=freDictpath
    wc=WordCloud(font_path='font/simhei.ttf',#設(shè)置字體  #simhei
                background_color=back, #背景顏色
                max_words=1300,# 詞云顯示的最大詞數(shù)
                max_font_size=120, #字體最大值
                margin=3,  #詞云圖邊距
                width=1800,  #詞云圖寬度
                height=800,  #詞云圖高度
                random_state=42)
    wc.generate_from_frequencies(fre_dict)  #從詞頻字典生成詞云
    plt.figure()  
    plt.imshow(wc)
    plt.axis("off")
    wc.to_file(savepath)
            
          

? ? ? 圖像數(shù)據(jù)結(jié)果如下:
?

python詞云可視化方法總結(jié)記錄【簡單詞云+背景圖片詞云+自定義字體顏色詞云】_第2張圖片

2、 基于背景圖像數(shù)據(jù)的詞云可視化具體實(shí)現(xiàn)如下:

? ? 先貼一下背景圖像:

python詞云可視化方法總結(jié)記錄【簡單詞云+背景圖片詞云+自定義字體顏色詞云】_第3張圖片

? ? ?這也是一個比較經(jīng)典的圖像數(shù)據(jù)了,下面來看具體的實(shí)現(xiàn):

            
              def simpleWC2(sep=' ',back='black',backPic='a.png',freDictpath='data_fre.json',savepath='res.png'):
    '''
    詞云可視化Demo【使用背景圖片】
    '''
    try:
        with open(freDictpath) as f:
            data=f.readlines()
            data_list=[one.strip().split(sep) for one in data if one]
        fre_dict={}
        for one_list in data_list:
            fre_dict[unicode(one_list[0])]=int(one_list[1])
    except:
        fre_dict=freDictpath
    back_coloring=imread(backPic)
    wc=WordCloud(font_path='simhei.ttf',#設(shè)置字體  #simhei
                background_color=back,max_words=1300,
                mask=back_coloring,#設(shè)置背景圖片
                max_font_size=120, #字體最大值
                margin=3,width=1800,height=800,random_state=42,)
    wc.generate_from_frequencies(fre_dict)  #從詞頻字典生成詞云
    wc.to_file(savepath)
            
          

? ? ? 結(jié)果圖像數(shù)據(jù)如下:

python詞云可視化方法總結(jié)記錄【簡單詞云+背景圖片詞云+自定義字體顏色詞云】_第4張圖片

3、 自定義詞云字體顏色的具體實(shí)現(xiàn)如下:
?

            
              #自定義顏色列表
color_list=['#CD853F','#DC143C','#00FF7F','#FF6347','#8B008B','#00FFFF','#0000FF','#8B0000','#FF8C00',
            '#1E90FF','#00FF00','#FFD700','#008080','#008B8B','#8A2BE2','#228B22','#FA8072','#808080']


def simpleWC3(sep=' ',back='black',freDictpath='data_fre.json',savepath='res.png'):
    '''
    詞云可視化Demo【自定義字體的顏色】
    '''
    #基于自定義顏色表構(gòu)建colormap對象
    colormap=colors.ListedColormap(color_list)  
    try:
        with open(freDictpath) as f:
            data=f.readlines()
            data_list=[one.strip().split(sep) for one in data if one]
        fre_dict={}
        for one_list in data_list:
            fre_dict[unicode(one_list[0])]=int(one_list[1])
    except:
        fre_dict=freDictpath
    wc=WordCloud(font_path='font/simhei.ttf',#設(shè)置字體  #simhei
                background_color=back,  #背景顏色
                max_words=1300,  #詞云顯示的最大詞數(shù)
                max_font_size=120,  #字體最大值
                colormap=colormap,  #自定義構(gòu)建colormap對象
                margin=2,width=1800,height=800,random_state=42,
                prefer_horizontal=0.5)  #無法水平放置就垂直放置
    wc.generate_from_frequencies(fre_dict)
    plt.figure()  
    plt.imshow(wc)
    plt.axis("off")
    wc.to_file(savepath)
            
          

? ? ? 結(jié)果圖像數(shù)據(jù)如下:

python詞云可視化方法總結(jié)記錄【簡單詞云+背景圖片詞云+自定義字體顏色詞云】_第5張圖片

? ? ? 上述三種方法就是我在具體工作中使用頻度最高的三種詞云可視化展示方法了,下面貼出來完整的代碼實(shí)現(xiàn),可以直接拿去跑的:

            
              #!usr/bin/env python
#encoding:utf-8
from __future__ import division

'''
__Author__:沂水寒城
功能: 詞云的可視化模塊
'''

import os
import sys
import json
import numpy as np
from PIL import Image
from scipy.misc import imread
from matplotlib import colors
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
from wordcloud import WordCloud,ImageColorGenerator,STOPWORDS

reload(sys)
sys.setdefaultencoding('utf-8')

#自定義顏色列表
color_list=['#CD853F','#DC143C','#00FF7F','#FF6347','#8B008B','#00FFFF','#0000FF','#8B0000','#FF8C00',
            '#1E90FF','#00FF00','#FFD700','#008080','#008B8B','#8A2BE2','#228B22','#FA8072','#808080']



def simpleWC1(sep=' ',back='black',freDictpath='data_fre.json',savepath='res.png'):
    '''
    詞云可視化Demo
    '''
    try:
        with open(freDictpath) as f:
            data=f.readlines()
            data_list=[one.strip().split(sep) for one in data if one]
        fre_dict={}
        for one_list in data_list:
            fre_dict[unicode(one_list[0])]=int(one_list[1])
    except:
        fre_dict=freDictpath
    wc=WordCloud(font_path='font/simhei.ttf',#設(shè)置字體  #simhei
                background_color=back, #背景顏色
                max_words=1300,# 詞云顯示的最大詞數(shù)
                max_font_size=120, #字體最大值
                margin=3,  #詞云圖邊距
                width=1800,  #詞云圖寬度
                height=800,  #詞云圖高度
                random_state=42)
    wc.generate_from_frequencies(fre_dict)  #從詞頻字典生成詞云
    plt.figure()  
    plt.imshow(wc)
    plt.axis("off")
    wc.to_file(savepath)


def simpleWC2(sep=' ',back='black',backPic='a.png',freDictpath='data_fre.json',savepath='res.png'):
    '''
    詞云可視化Demo【使用背景圖片】
    '''
    try:
        with open(freDictpath) as f:
            data=f.readlines()
            data_list=[one.strip().split(sep) for one in data if one]
        fre_dict={}
        for one_list in data_list:
            fre_dict[unicode(one_list[0])]=int(one_list[1])
    except:
        fre_dict=freDictpath
    back_coloring=imread(backPic)
    wc=WordCloud(font_path='simhei.ttf',#設(shè)置字體  #simhei
                background_color=back,max_words=1300,
                mask=back_coloring,#設(shè)置背景圖片
                max_font_size=120, #字體最大值
                margin=3,width=1800,height=800,random_state=42,)
    wc.generate_from_frequencies(fre_dict)  #從詞頻字典生成詞云
    wc.to_file(savepath)


def simpleWC3(sep=' ',back='black',freDictpath='data_fre.json',savepath='res.png'):
    '''
    詞云可視化Demo【自定義字體的顏色】
    '''
    #基于自定義顏色表構(gòu)建colormap對象
    colormap=colors.ListedColormap(color_list)  
    try:
        with open(freDictpath) as f:
            data=f.readlines()
            data_list=[one.strip().split(sep) for one in data if one]
        fre_dict={}
        for one_list in data_list:
            fre_dict[unicode(one_list[0])]=int(one_list[1])
    except:
        fre_dict=freDictpath
    wc=WordCloud(font_path='font/simhei.ttf',#設(shè)置字體  #simhei
                background_color=back,  #背景顏色
                max_words=1300,  #詞云顯示的最大詞數(shù)
                max_font_size=120,  #字體最大值
                colormap=colormap,  #自定義構(gòu)建colormap對象
                margin=2,width=1800,height=800,random_state=42,
                prefer_horizontal=0.5)  #無法水平放置就垂直放置
    wc.generate_from_frequencies(fre_dict)
    plt.figure()  
    plt.imshow(wc)
    plt.axis("off")
    wc.to_file(savepath)



if __name__ == '__main__':
    text="""
        The Zen of Python, by Tim Peters
        Beautiful is better than ugly.
        Explicit is better than implicit.
        Simple is better than complex.
        Complex is better than complicated.
        Flat is better than nested.
        Sparse is better than dense.
        Readability counts.
        Special cases aren't special enough to break the rules.
        Although practicality beats purity.
        Errors should never pass silently.
        Unless explicitly silenced.
        In the face of ambiguity, refuse the temptation to guess.
        There should be one-- and preferably text one --obvious way to do it.
        Although that way may not be obvious at first unless you're Dutch.
        Now is better than never.
        Although never is often better than *right* now.
        If the implementation is hard to explain, it's a bad idea.
        If the implementation is easy to explain, it may be a good idea.
        Namespaces are one honking great idea -- let's do more of those!
        """
    word_list=text.split()
    fre_dict={}
    for one in word_list:
        if one in fre_dict:
            fre_dict[one]+=1
        else:
            fre_dict[one]=1
    simpleWC1(sep=' ',back='black',freDictpath=fre_dict,savepath='simpleWC1.png')
    simpleWC2(sep=' ',back='black',backPic='backPic/A.png',freDictpath=fre_dict,savepath='simpleWC2.png')
    simpleWC3(sep=' ',back='black',freDictpath=fre_dict,savepath='simpleWC3.png')
            
          

? ? ? ?記錄一下,歡迎交流學(xué)習(xí)。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 狠狠综合久久久久综合小说网 | 欧美色视频日本片高清在线观看 | 久久天天躁狠狠躁夜夜爽 | 久久国产免费一区二区三区 | 亚洲欧美另类专区 | 日本免费在线视频 | 亚洲伊人色一综合网 | 久久综合久久久久 | 中文字幕专区高清在线观看 | 精品久久久久久久高清 | 青草青草久热精品视频99 | 一级毛片 在线播放 | 国产午夜精品久久久久九九 | 亚洲精品国产字幕久久不卡 | 乱人伦中文视频在线 | 天天综合天天综合 | 国产精品久久天天影视 | 亚洲欧美中文在线观看4 | 久久成人免费 | 国产精品日韩在线观看 | 天天操天天干天天玩 | 欧美成人精品高清在线播放 | 亚洲精品中文一区不卡 | 青春草禁区视频在线观看 | 999资源| 麻豆久久精品免费看国产 | 欧美专区在线观看 | 久久97久久97精品免视看 | 黄色成人一级片 | 亚洲一级毛片免费在线观看 | 亚洲成人看片 | 成人私人影院在线观看网址 | 操美女在线 | 高清黄色毛片 | 在线观看免费av网站 | 深夜色| 福利在线视频一区热舞 | 亚洲欧美日韩高清一区二区三区 | 国产成人18黄禁网站免费观看 | 99热在线获取最新地址 | 成人区在线观看免费视频 |