Word Cloud Fantasy: Shaping Data with Mathematics and Art
Data visualization is often seen as a purely technical field—a world of bar charts, scatter plots, and linear regressions. But what if you could go beyond the usual rectangular layouts and create shapes that carry meaning, emotion, and storytelling power? That’s where shape‑based word clouds come in. Word clouds have always been a fun and visually appealing way to represent text data.
In this post, I’ll walk you through how to generate a heart‑shaped word cloud using Python, NumPy, Matplotlib, and the WordCloud library.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud
The Math Behind the Heart
Instead of using a pre‑drawn image, we generate the heart shape using the algebraic heart equation:
(x^2+y^2-1)^3-x^2y^3=0
This equation produces a smooth, symmetric heart curve. We use np.ogrid to create a scaled grid ranging from −2 to 2. This ensures our heart is perfectly centered and smooth.
# Create a scaled grid using complex numbers (800j)
x, y = np.ogrid[-2:2:800j, -2:2:800j]
# The Heart Equation
heart = (x**2 + y**2 - 1)**3 - x**2 * y**3
Crafting the Mask
A WordCloud mask works like a stencil. In the Python wordcloud library, the generator looks for values of 0 to know where to draw and 255 to know where to stop. We create a "blank" canvas of 255s (white space) and then "carve out" the heart by setting all coordinates where the equation result is ≤0 to 0. By using np.rot90(mask, k=1), we flip the heart 90 degrees to the left to show it upright.
# Create a mask where 255 is the background (non-drawable) and 0 is the heart (drawable)
mask = np.full(heart.shape, 255, dtype=np.uint8) mask[heart <= 0] = 0
# Rotate the mask 90 degrees to the left
mask_rotated = np.rot90(mask, k=1)
Then we pass it to the WordCloud object. We’ve chosen a "Reds" colormap and a "darkred" contour to emphasize the heart theme. Finally, we generate the cloud from a dictionary of word frequencies.
# Generate the word cloud
wc = WordCloud( background_color="white", mask=mask_rotated, colormap="Reds", contour_width=2,
contour_color="darkred" ) wc.generate_from_frequencies(freq)
# Display
plt.figure(figsize=(8, 8))
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.title("Heart-Shaped Word Cloud", fontsize=24)
plt.show()
Why "WordCloud Fantasy" Matters
This approach proves that data scientists don't have to be limited by "Standard" shapes. By understanding the underlying math of a mask, you can create word clouds in the shape of corporate logos, geographic maps, or even abstract symbols.
The next time you’re presenting qualitative data, don’t just show a list. Give your audience a bit of a fantasy—show them the heart of the data.


