Change Font Size

Follow the steps below to change the font size of the words in the word cloud-

  • First, select a font from this collection.
  • Download the font file—you see that it is in .zip format.
  • Open the zip file and extract the TTF or otf file from it.
  • Now just provide the full path of the TTF or otf file.
path = r'C:\Users\Dell\Downloads\Garbage\Candy Beans.otf'

Complete Code:

Python3




# Python3 program to implement
# the above approach
# Import the following modules
 
# pip install matplotlib
import matplotlib.pyplot as plt 
 
# pip install wordcloud
from wordcloud import WordCloud, STOPWORDS 
import numpy as np
from PIL import Image
 
# Give the whole path of the text file,
# open it, read it, and encode it.
text = open(r'C:\Users\Dell\Desktop\Wordcloud\Text.txt',
            mode = 'r', encoding = 'utf-8').read() 
 
# For changing the fonts of wordcloud fonts
path = r'C:\Users\Dell\Downloads\Garbage\Candy Beans.otf'
 
# The Image shape in which you wanna convert it to.
mask = np.array(Image.open(
                r'C:\Users\Dell\Downloads\Garbage\GFG.png'))
 
# Now inside the WordCloud, provide some functions:
# stopwords - For stopping the unuseful words
# like [,?/\"]
# font_path - provide the font path to which
# you wanna convert it to.
# max_words - Maximum number of words in
# the output image.
# Also provide height and width of the mask
wc = WordCloud(stopwords = STOPWORDS,
               font_path = path,
               mask = mask,
               background_color = "white",
               max_words = 2000,
               max_font_size = 500,
               random_state = 42,
               width = mask.shape[1],
               height = mask.shape[0])
 
# Finally generate the wordcloud of the given text
wc.generate(text) 
plt.imshow(wc, interpolation = "None")
 
# Off the x and y axis
plt.axis('off'
 
# Now show the output cloud
plt.show()


Output:

Generate Word Clouds Of Any Shape In Python

In this article, we will discuss how to create word clouds of any shape in Python. 

The term WordCloud refers to a data visualization technique for showing text data in which the size of each word indicates its frequency or relevance. To create a word cloud of any shape, use Python’s Matplotlib, word cloud, NumPy, and PIL packages.

Similar Reads

Stepwise Implementation

Let’s have a look at the step-by-step implementation –...

Change Font Size

...

Change the Font Color

Follow the steps below to change the font size of the words in the word cloud-...

Contact Us