保留正文与补采附件。源页面当前未重新核验;具体限制见交付清单。

Revisit consent button We value your privacy We use cookies to enhance your browsing experience, serve personalised ads or content, and analyse our traffic. By clicking "Accept All", you consent to our use of cookies. Customise Reject All Accept All Customise Consent Preferences We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below. The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... Show more NecessaryAlways Active Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

Functional Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

Analytics Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

Performance Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Advertisement Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.

Uncategorised Other uncategorised cookies are those that are being analysed and have not been classified into a category as yet. No cookies to display. Reject All Save My Preferences Accept All Toggle SearchToggle Mobile Navigation Publish AI, ML & data-science insights to a global community of data professionals. Sign inSubmit an Article LatestEditor's PicksNewsletterShipAI


Write For TDS LinkedInXToggle Search Data Visualization

Data Storytelling with Animated Word Clouds

Animated word clouds turn classic word clouds into a dynamic visualization. Learn more about telling data stories in Python. Petr Koráb November 22, 20236 min read Learn this step by step with the interactive Data Analyst roadmap.

Introduction

An animated word cloud displays absolute frequencies of n-grams (contiguous sequences of text sample items) over time as a sequence of images in a video file. It **** gives greater importance to words that appear more frequently in a source text. The bigger and bolder the n-gram displays, the more frequently it appears in the text. It builds on the intuitive logic of classic word clouds and adds a time perspective to the visualization. As many text datasets are collected these days as text observations over multiple periods, there is a particular challenge to visualize the changes in the data over time. Instead of making summary tables or graphs for many different periods, let's prepare an MP4 video that tells the story, attracts the audience, and gives a "wow" effect to the presentation. This article will describe the generation of animated word clouds from text data in Python. Here are some unique features of the AnimatedWordCloud library:

Please accept cookies to access this content

How to use it

To use the library, follow these steps:

1. Installation

Create a new Python 3.8 virtual environment for the project to avoid any dependence conflicts. AnimatedWordCloud relies on Python 3.8 because of its visualization requirements. To install using pip, use: text

pip install AnimatedWordCloud

It was tested with Pycharm community ed. It's recommended to use this IDE and run the code in a .py file instead of a jupyter notebook.

2. Generate frames

We will focus on the European Central Bank (ECB) communication and figure out the concepts the Bank Board members discussed over 1997–2023. The dataset is from the ECB website, released with a flexible license. The data contains 2846 rows and has NaN values that AnimatedWordCloud can effectively process. It looks like this: Let's import the data. text

import pandas as pd

data = pd.read_csv('dataset.csv')

And then import the _animated_word cloud method.

EDIT Dec 2023 : AnimatedWordCloud has been constantly updated with new parameters. Check PiPy for the current release. It offers reading data in US (MM/DD/YYYY) or European-style (DD/MM/YYYY) date and datetime formats. It automatically cleans data from punctuation and numbers on input. It can also remove the standard list(s) of stopwords for languages in the NLTK corpus of stopwords. text

from AnimatedWordCloud import animated_word_cloud

animated_word_cloud(text=data['contents'],  # Read text column
          time=data['date'],                # Read date column
          date_format = 'us',               # Read dates in US date format
          ngram=1,                          # Show individual word frequencies
          freq='Y',                         # Calculate yearly frequencies
          stopwords=['english', 'french',   # Clean from English, French,
                     'german', 'spanish'])  # German and Spanish stop words

The code generates 90 PNG frames per period and creates a .postprocessing/frames folder in the working directory to store the images.

3. Create a video from images

The last step is to make a video file from individual frames. This step will be automated in future releases, but for now: Download the ffmpeg folder and the frames2video.bat file from here and place them into the postprocessing folder. Next, run frames2video.bat , which will generate a wordSwarmOut.mp4 file, which is the desired output. Please accept cookies to access this content Let's tell a story of the Eurozone through the lenses of central bankers:

These developments are obvious to anyone interested in the history of the Euro. But to present them is a challenging task. We can use, for example, (1) n-gram frequency analysis of keywords and produce many frequency tables of keywords, or (2) display a heatmap (or a matrix graph) with a period on the x-axis , word on the y-axis , and the word frequency the item in the matrix. Another option (3) is to produce many word clouds for each period. None of these is perfect for larger datasets, and the animated word cloud gives you another option for delivering the message.

Practical applications

The library has a primary use in presentations and teaching. Text mining is now blending other disciplines like Economics, Politology, or Business, and teachers, analysts, and students can now use a different, more appealing way to present the facts.

Our earlier TDS article with Jarko Fidrmuc and David Štrba outlined how word clouds are useful for summarization and exploratory text data analysis. In a dynamic form, it is possible to present the structure of text datasets collected as time series in a better, easily understandable way. Other interesting use cases, like modeling COVID-19-related discussions or US presidential debate analysis with X-tweets, might also be interesting to check in a bachelor or seminar thesis.

On a technical note

In the WordsSwarm project, Michael Kane developed the core framework for the animation of word frequencies for the library. AnimatedWordCloud, which I have created with an Apache-2.0 license, makes the codes efficiently work on various text datasets of the Latin alphabet languages. It uses one of my earlier projects, the 𝐀𝐫𝐚𝐛𝐢𝐜𝐚 library, to make the processing (text cleaning and word frequency aggregation. It relies on rather archaic visualization requirements that suit this project very well. PyBox2D is used for physics and clash detection of words in the swarm. Pyglet, cross -platform windowing and multimedia library, and PyGame are used to create animations. It shows absolute word frequencies but scales the data to display word clouds on datasets of different sizes. In this way, datasets with large frequencies are scaled by a constant so that the word clouds can still be displayed in a video. It handles missing values and also treats mojibake errors with ftfy . On the other hand, AnimatedWordCloud might have difficulties running in a Jupyter notebook and other IDEs than Pycharm. I will have a look at these shortcomings in the next releases. In future releases, AnimatedWordCloud will also provide bigram frequencies that will give a more detailed look at the narratives and topics people discussed in the texts. Also, I will extend the library's documentation. PS: Let me know how it worked on your projects! :) If you enjoy my work, you can invite me for coffee and support my writing. You can also subscribe to my email list to get notified about my new articles. Thanks!

Register Now to Continue Reading

Unlock Towards Data Science with a Free Account

All articles on Towards Data Science are available to registered users. Create a free account today to access this article and thousands more. Register for freeSign in to your account


Written By

Petr Koráb

See all from Petr Koráb Data StorytellingData VisualizationPythonText MiningWord Cloud Share This Article

Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program. Write for TDS

Related Articles

Visualising Well Paths on 3D Line Plots with Plotly Express

Data Science Working With Plotly Express 3D Line Plots Andy McDonald June 4, 20237 min read

Simple Versioned Datasets With Github Actions

Programming In Less Than 10 Lines Of Python - Preserving The History of Notable CEOs Dylan Roy October 12, 20206 min read

The World's Smallest Data Pipeline Framework

Data Science A simple and fast data pipeline foundation with sophisticated functionality. Douglas Blank November 16, 20237 min read

Pharos: How to Visualize Advanced Computer Vision Datasets with 4 Lines of Code

Machine Learning This free Python library could save you a ton of time. But is there a catch? Dario Radečić September 8, 20215 min read

Toronto on Fire in Data, Part 1

Data Science Fire Incidents Analysis by Segmentation & Poisson in Practice Anders Ohrn February 6, 20199 min read

Animate your Python Graphs with Pillow

Create animated gifs of your plots with the Python library Pillow to bring life to your insights. Samir Saci August 31, 20226 min read

What I learnt from giving 120+ Data Science Presentations

Data Science 6 points to help navigate the world and daily technical practices to become better data communicators. Das Wijesundera October 16, 20209 min read

How to Create a Polar Histogram With Python and Matplotlib

Programming Creating a graph that captures the eye and tells the beholder an insightful story. Oscar Leo August 24, 202310 min read

The Best Practice of Reading Text Files In Python

Artificial Intelligence Combine multiple files into a single stream with richer metadata Christopher Tao June 13, 20216 min read

Your home for data science and AI. The world's leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals. Subscribe to Our NewsletterWrite For TDS•About•Advertise•Privacy Policy•Terms of Use © Insight Media Group, LLC 2026•The New Stack•roadmap.sh•Creator Network