I need a wordcloud in which the same word could appear twice with different colors; red indicating negative associations, and green positive.
I am already able to generate a wordcloud with repeated words using MultiDicts (see the code below):
However, I need one of the homes to appear in green color. Is this possible with the wordcloud libray? Can somebody recommend another library that support this?
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')