top of page
Lekha Mirjankar

Beautify Word Clouds in Python

Good day, dearest readers!


In the previous post, we analyzed word frequencies in Jane Austen's Pride and Prejudice. Today, I wanted to beautify the visualization and customize the Word Cloud in the likeness of Lizzy and Darcy.


1. Import Modules

Import the required Python modules.

import numpy as np
from PIL import Image

Image is part of the Python Imaging Library (PIL) and is used to open and manipulate images. numpy is a powerful numerical computing library in Python.


2. Load the mask

pnp_mask = np.array(Image.open('./lizzy_darcy_wc.jpg'))
pnp_mask

Load the desired image file and convert it into a Numpy array which will serve as a mask for our Word Cloud.

Output:



3. Crafting the Word Cloud

wc = WordCloud(width=800, height=400, background_color='black', colormap="RdPu", mask=pnp_mask, contour_width=2, contour_color='white').generate_from_frequencies(dict(top_ten))
  • colormap: defines the color palette. We've chosen the "RdPu" colormap, ranging from red to purple.

  • mask: specifies the mask we loaded earlier, ensuring our Word Cloud takes the shape of the desired silhouette.

  • contour_width: add an outline to the mask

  • contour_color: specifies the color of the outline


4. The Grand Reveal

plt.figure(figsize=(10, 6))
plt.imshow(wc, interpolation='bilinear')
plt.axis('off')
plt.show()

Output:





This is day2 of my #100daysofdata challenge.

I was quite excited to try masking a Word Cloud and this seemed like the perfect opportunity!


Let me know if you have any suggestions or ideas for me.


Happy analyzing!

Recent Posts

See All

Extract Genre from Goodreads

After a brief hiatus because of my laptop issues; I'm back to the data challenge. Today, I continued to work on the NYT Bestsellers...

Comments


bottom of page