Python word cloud, need to control font size and colour
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Looking for an approach to specify font size and font colour separately in a wordcloud.

Solution must be able to:

Support word phrases, not just single words.

Specify Min and Max font sizes.

Assign colours based on 1) a colour range (light to dark) and 2) be able to optionally assign a specific colour to a value, i.e. cost[] in the example.

The example does not do anything with the cost[].

from wordcloud import WordCloud
import matplotlib.pyplot as plt
import pandas as pd

whiskey = ['Johnnie Walker Gold', 'Kilchoman Sauternes Cask', 'Ardbeg 10 Year',
         'Arran Robert Burns Single Malt', 'Ballantines Finest',
         'Macallan 12 Years', 'Glenlivet 15 Year ', 'Lagavulin 16 Years', 'Kilchoman Sauternes']

# Likes should represent font size
likes = [55, 83, 33, 42, 19, 23, 5, 69, 4]

# cost should represent font color
cost = [50, 23, 45, 23, 56, 55, 43, 32, 36] # ?

text = ''
for i, word in enumerate(whiskey):
    text = text + likes[i] * (word + ',') 

d = dict(zip(whiskey, likes))
wordcloud = WordCloud(collocations=False,width=600,height=400,background_color="white").generate_from _frequencies(d)

plt.imshow(wordcloud, interpolation = 'bilinear')
To control size you may use the generate_from_frequencies method, and the recolor method to change words color
drakmail 11 months ago
You can see example of coloring by the link:
drakmail 11 months ago
And custom frequency by the link:
drakmail 11 months ago

Crowdsource coding tasks.

1 Solution


Here is the complete script:

You can specify a custom color by modifying HSL values. Use a color picker to do that

max_font_size & min_font_size arguments are responsible for controling text font size.
VladimirMikulic 11 months ago
I don't understand this line: "hsl(0, 0%%, %d%%)" % cost[wordIndex], what is is doing? What changes when changing colors? It looks very elegant, but I am struggling with the syntax.
broadreach 11 months ago
Hi @broadreach. No problem at all! Feel free to ask anytime why I did something the way I did. random_color_func runs and gets a new word as an argument from whiskey array. Each item in the whiskey array has a corresponding "cost" at the same index in the cost array. We get the index of the word that has been passed to the function. Once we have the index of the word, we use it to reference the corresponding cost in the cost array. Here comes the magic part :) HSL is the format we use to represent a colour. It's just an alternative to RGB/HEX. %d acts as a placeholder for the value that we get from cost array. It's the third argument to the HSL format and it represents the lightness of the specified color.
VladimirMikulic 11 months ago
That is very cool. Thanks!
broadreach 11 months ago
The lightness ranges from 0 - 100 (black - white). I apologise for not documenting this in the source. Hopefully, this clears your doubt :)
VladimirMikulic 11 months ago