pub-3719887310684815
top of page

Create a wordcloud in Python

Aug 20

2 min read

0

5

0

Learn Python with fun libraries like Wordcloud and Beautifulsoup to create fun projects like this image of Taylor Swift's lyrics from the 00's.


One of the fustrating things about python is every time I read a tutorial to do something, the libraries don't work as the tutorial suggests. So, I wanted to show you how I ended up doing it.


Here is what I used to create the image above:

  • from os import path

  • from PIL import Image

  • import numpy as np

  • import matplotlib.pyplot as plt

  • import os

  • from wordcloud import WordCloud


and these assignments:

  • d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()

  • Guitar_mask = np.array(Image.open(path.join(d, r"C:\GuitarMask.png")))


The function I used is:

def plot_wordcloud(df,row,col):

wc = WordCloud(background_color="white",colormap="Dark2",

max_font_size=100,random_state=15,mask=Guitar_mask)

fig = plt.figure(figsize=(20,10))

for index, value in enumerate(df.columns[1:]):

top_dict = dict(zip(df['words'].tolist(),df[value].tolist()))

wc.generate_from_frequencies(top_dict)

plt.subplot(row,col,index+1)

plt.imshow(wc,interpolation="bilinear")

plt.axis("off")

plt.title(f"{value}",fontsize=15)

plt.subplots_adjust(wspace=0.1, hspace=0.1)

plt.show() ##Plot the word cloud.

plot_wordcloud(vect_words,2,2)

plt.savefig("wcsaved.png") ## I like saving the plots as a file instead of only displaying it.


Then the function call:


plot_wordcloud(vect_words,2,2)




The lessons I would give here is to plan ahead as to the font size, number of columns and rows, and the mask. A mask isn't needed, but it usually helps, and makes the wordcloud look like some intention is given.


Other than that, make sure you place the call to make the wordcloud in the right place between working with the dataframe. I had to strip stop words and extra song references like "Solo", "Chorus", and words like that. But, I also used it to troubleshoot my the efficiency of the removal of those words. There are so many words in 100 song lyrics, one has to visualize the troubleshooting process.


I make the guitar mask in GIMP. Make sure the mask is black and white. Black areas will show words, and white areas will not.

Comments

Share Your ThoughtsBe the first to write a comment.
bottom of page