【问题标题】:How to create a wordcloud with the same word several times but with different colors?如何多次创建具有相同单词但颜色不同的wordcloud?
【发布时间】:2017-12-14 17:48:56
【问题描述】:

我需要一个词云,其中同一个词可以用不同的颜色出现两次;红色表示负相关,绿色表示正。

我已经能够使用 MultiDicts 生成包含重复单词的 wordcloud(参见下面的代码):

但是,我需要其中一栋房屋以绿色显示。 wordcloud libray可以做到这一点吗?有人可以推荐另一个支持这个的库吗?


from wordcloud import WordCloud
from multidict import MultiDict

class WordClouder(object):

    def __init__(self, words, colors):
        self.words = words
        self.colors = colors

    def get_color_func(self, word, **args):
        return self.colors[word]

    def makeImage(self, path):
        wc = WordCloud(background_color="white", width=200, height=100)

        # generate word cloud
        wc.generate_from_frequencies(self.words)

        # color the wordclound
        wc.recolor(color_func=self.get_color_func)

        image = wc.to_image()
        image.save(path)


if __name__ == '__main__':

    # Ideally this would be used somewhere
    colors_valence = {
        '+': 'green',
        '-': 'red',
    }

    colors = {
        'home': 'red',
        'car': 'green',
    }

    words = MultiDict({
        'home': 2.0, # This home should be green
        'car': 20.0,
    })

    words.add('home',10) , # This home should be red


    wc = WordClouder(words, colors)
    wc.makeImage('wordcloud.png')

【问题讨论】:

  • 你看懂你写的代码了吗? def get_color_func 根据确切的单词返回颜色。所以要么创建一个虚拟词home 并赋予它另一种颜色,或者将整个词 颜色系统重写为其他东西。
  • 你的意思是在首页添加一个空格,所以我有"home ""home"?我想过,但实际上我将有两个以上的类别(正面和负面只是一个简化的例子,使问题易于理解)。如果我有 5 个类别,就会有 4 个空格,然后词云开始看起来很奇怪。无论如何,这不是一个非常优雅的解决方案,更像是一个 hack。
  • 然后,正如我所说,您需要添加另一种区分变体的方法。就像现在一样,颜色与这个词有着千丝万缕的联系。
  • 嗯,是的,这是我的问题。如果 wordcloud 库支持这一点,或者有人可以推荐替代方案。我查看了wordcloud代码,似乎不支持。
  • 我想过给字符串添加属性,但它是not very recommended,另一个猴子补丁。

标签: python word-cloud


【解决方案1】:

我通过继承 WordCloud 创建了一个不错的解决方案。您可以复制并粘贴下面的代码,然后您可以使用 + 和 - 来表示颜色,如下所示:

words = {
    'home+': 2.0,
    'home-': 10.0,
    'car+': 5.0,
}

会生成这个:



解释:我在字典中告诉我颜色的单词后面添加了一个字符(+-)。我删除了我覆盖的WordCloudrecolor 方法中的字符,但我确实将整个世界(包括字符+ 或-)发送到我用来选择适当颜色的color_fun。我在下面的代码中注释了重要的部分。

from wordcloud import WordCloud as WC

from multidict import MultiDict


class WordCloud(WC):

    def recolor(self, random_state=None, color_func=None, colormap=None):
        if isinstance(random_state, int):
            random_state = Random(random_state)
        self._check_generated()

        if color_func is None:
            if colormap is None:
                color_func = self.color_func
            else:
                color_func = colormap_color_func(colormap)

        # Here I remove the character so it doesn't get displayed
        # when the wordcloud image is produced
        self.layout_ = [((word_freq[0][:-1], word_freq[1]), font_size, position, orientation,
               # but I send the full word to the color_func
               color_func(word=word_freq[0], font_size=font_size,
                          position=position, orientation=orientation,
                          random_state=random_state,
               font_path=self.font_path))
               for word_freq, font_size, position, orientation, _
                   in self.layout_]

        return self


class WordClouder(object):

    def __init__(self, words, colors):
        self.words = words
        self.colors = colors

    def get_color_func(self, word, **args):
        return self.colors[word[-1]]

    def makeImage(self, path):
        #alice_mask = np.array(Image.open("alice_mask.png"))

        wc = WordCloud(background_color="white", width=200, height=100)

        # generate word cloud
        wc.generate_from_frequencies(self.words)

        # color the wordclound
        wc.recolor(color_func=self.get_color_func)

        image = wc.to_image()
        image.save(path)


if __name__ == '__main__':

    colors = {
        '+': '#00ff00',
        '-': '#ff0000',
    }

    words = {
        'home+': 2.0,
        'home-': 10.0,
        'car+': 5.,
    }

    wc = WordClouder(words, colors)
    wc.makeImage('wc.png')

【讨论】:

    猜你喜欢
    • 2013-10-08
    • 2017-07-03
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多