WordCloud is a Python library for generating visual representations of text data, commonly known as word clouds or tag clouds. It takes text as input and visualizes the most frequent words, with the size of each word indicating its frequency. The library is actively maintained, with frequent updates, and focuses on efficient algorithms, flexible masking, and filling available space. The current version is 1.9.6.
pip install wordcloudVerified import paths — ran on the pinned version, not inferred.
This quickstart generates a basic word cloud from a sample string and displays it using Matplotlib. It demonstrates the core `WordCloud` class instantiation, text generation, and image display.
Ensure you have a C compiler installed (e.g., build-essential on Linux, Xcode command-line tools on macOS, Visual C++ Build Tools on Windows) or check for compatible pre-built wheels for your environment.
Check the `wordcloud` GitHub issues for updates and potential fixes. If encountering issues, try downgrading `matplotlib` to a known compatible version (e.g., 3.8.x) or upgrading `wordcloud` to its latest version (1.9.6 or newer) which might include a fix.
Always include `plt.axis('off')` before `plt.show()` when displaying a `wordcloud` image with `matplotlib`.Upgrade `wordcloud` to the latest version (1.9.6 or newer) to ensure compatibility with recent `Pillow` versions. If unable to upgrade `wordcloud`, consider pinning `Pillow` to a version older than 10.
On Windows, install the 'Desktop development with C++' workload via Visual Studio Installer, or directly download 'Microsoft C++ Build Tools' from the provided link.
Load the mask image using `Pillow` and `numpy`, ensuring it's converted to a NumPy array and transparent pixels are set to 255 to act as a valid mask (e.g., `mask = np.array(Image.open('mask.png')); mask[mask.ndim-1 == 0] = 255`).Prepare your data as a dictionary like `{'word1': 10, 'word2': 5, 'word3': 12}` and pass this dictionary directly: `wordcloud.generate_from_frequencies(your_frequency_dict)`.Ensure the `font_path` points to a valid and accessible `.ttf` or `.otf` font file on your system (e.g., `WordCloud(font_path='/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf')`).