您当前的位置: 首页 > 

IT之一小佬

暂无认证

  • 0浏览

    0关注

    1192博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

词云的美化

IT之一小佬 发布时间:2021-03-28 14:56:02 ,浏览量:0

词云的美化

scipy库中的imread被弃用也就是不能用了,这时需要改成这样`from imageio import imread`

’射雕背景1.png‘

from imageio import imread
import matplotlib.pyplot as plt
import pandas as pd

myfont = myfont = r'C:\Windows\Fonts\simhei.ttf'
text = chapter.txt[1]  #  在上方处理后拿到的数据
pd_stop_word = pd.read_csv('../data/停用词.txt',  names=['w', 'z'], encoding='utf-8')
pd_stop_word_list = list(pd_stop_word.w.astype(str))  #  加上astype(str),防止报错

def m_cut(text):
    return [word for word in jieba.cut(text) if word not in pd_stop_word_list and len(word) > 1]

cloudobj = wordcloud.WordCloud(font_path=myfont, mask=imread('../data/射雕背景1.png'), mode='RGBA', background_color=None).generate(' '.join(m_cut(text)))

plt.imshow(cloudobj)
plt.axis('off')
plt.show()

from imageio import imread
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

myfont = myfont = r'C:\Windows\Fonts\simhei.ttf'
imgobj = imread('../data/射雕背景1.png')
img_color = wordcloud.ImageColorGenerator(np.array(imgobj))
text = chapter.txt[1]  #  在上方处理后拿到的数据
pd_stop_word = pd.read_csv('../data/停用词.txt',  names=['w', 'z'], encoding='utf-8')
pd_stop_word_list = list(pd_stop_word.w.astype(str))  #  加上astype(str),防止报错

def m_cut(text):
    return [word for word in jieba.cut(text) if word not in pd_stop_word_list and len(word) > 1]

cloudobj = wordcloud.WordCloud(font_path=myfont, mask=imread('../data/射雕背景1.png'), mode='RGBA', background_color=None).generate(' '.join(m_cut(text)))

cloudobj.recolor(color_func=img_color)
plt.imshow(cloudobj)
plt.axis('off')
plt.show()

#  指定词云单词颜色
from wordcloud import get_single_color_func

class GroupedColorFunc(object):
    """Create a color function object which assigns DIFFERENT SHADES of
       specified colors to certain words based on the color to words mapping.
 
       Uses wordcloud.get_single_color_func
 
       Parameters
       ----------
       color_to_words : dict(str -> list(str))
         A dictionary that maps a color to the list of words.
 
       default_color : str
         Color that will be assigned to a word that's not a member
         of any value from color_to_words.
    """
 
    def __init__(self, color_to_words, default_color):
        self.color_func_to_words = [
            (get_single_color_func(color), set(words))
            for (color, words) in color_to_words.items()]
 
        self.default_color_func = get_single_color_func(default_color)
 
    def get_color_func(self, word):
        """Returns a single_color_func associated with the word"""
        try:
            color_func = next(
                color_func for (color_func, words) in self.color_func_to_words
                if word in words)
        except StopIteration:
            color_func = self.default_color_func
 
        return color_func
 
    def __call__(self, word, **kwargs):
        return self.get_color_func(word)(word, **kwargs)


color_to_words = {
    '#00ff00': ['颜烈', '武官', '金兵', '官兵'],
    'red': ['包惜弱', '郭啸天', '杨铁心', '丘处机']
}
default_color = 'grey'  #  指定其它词条的颜色
group_color_func = GroupedColorFunc(color_to_words, default_color)

cloudobj.recolor(color_func=group_color_func)

plt.imshow(cloudobj)
plt.axis('off')
plt.show()

GroupedColorFunc()方法的代码直接在官网复制就好

 

关注
打赏
1665675218
查看更多评论
立即登录/注册

微信扫码登录

0.0391s