#261 Custom python wordcloud

 

 

 

 

 

The chart #260 aims to describe how to perform a basic wordcloud. This page shows how to customize it. Here are a few tips. Note that each chart always starts the same way:

 

 

 

 

 
# Libraries
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# Create a list of word
text=("Python Python Python Matplotlib Matplotlib Seaborn Network Plot Violin Chart Pandas Datascience Wordcloud Spider Radar Parrallel Alpha Color Brewer Density Scatter Barplot Barplot Boxplot Violinplot Treemap Stacked Area Chart Chart Visualization Dataviz Donut Pie Time-Series Wordcloud Wordcloud Sankey Bubble")

 

Maximum and minimum font size

 

 

You can control minimum and maximum font size of your wordcloud. For example, let’s imagine you want only words written in small font:

 

 

 

 
wordcloud = WordCloud(width=480, height=480, max_font_size=20, min_font_size=10).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.margins(x=0, y=0)
plt.show()
Number of words

 

 

It is possible to set a maximum number of words to display on the tagcloud. Let’s assume I want to show only the 3 most frequent words:

 

 

 

 
wordcloud = WordCloud(width=480, height=480, max_words=3).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.margins(x=0, y=0)
plt.show()
Remove some words

 

 

You can remove some words that don’t interest you. Just list them as follow:

 

 

 

wordcloud = WordCloud(width=480, height=480, stopwords=["Python", "Matplotlib"]).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.margins(x=0, y=0)
plt.show()
Change background

 

 

Change the color of the background of your python wordcloud:

 

 

 

 
wordcloud = WordCloud(width=480, height=480, background_color="skyblue").generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.margins(x=0, y=0)
plt.show()
Change color of words

 

 

And finally change the color of words using whatever palette!

 

 

 

 
wordcloud = WordCloud(width=480, height=480, colormap="Blues").generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.margins(x=0, y=0)
plt.show()

Leave a Reply

Your email address will not be published.