Creating Interactive and Animated Charts with ipyvizzu

Devashree Madhugiri 24 Mar, 2023 • 8 min read

Introduction

Data visualization plays a crucial role in analyzing and interpreting data. It helps to identify patterns, trends, and relationships in large and complex datasets, making it easier to communicate insights and findings to a broader audience. With the growing importance of data science and machine learning, data analysis holds a special place in many industries and fields. This article will introduce ipyvizzu, a Python visualization package that allows the creation of animated analysis inside the notebook for presentation.

The results of data analysis are presented to the stakeholders, storytelling is a key skill in data analysis. In order to effectively explain the results of data analysis, one can use the findings to tell a story through data. Using animations to communicate helps the audience to note the highlighted information and absorb the intended message in the data story.

Learning Objectives

  1. Learn about ipyvizzu and its main features
  2. Review some real-world applications for ipyvizzu
  3. Understand the foundation of ipyvizzu, including its animation states and the types of non-keyword arguments
  4. Know the different ways to work with data in ipyvizzu
  5. Learn how to build charts using ipyvizzu
  6. Understand how to animate charts with ipyvizzu

This article was published as a part of the Data Science Blogathon

Table of Contents

What is ipyvizzu?

Ipyvizzu is a powerful animated visualization tool that works with notebooks like Jupyter, Google Colab, Databricks, Kaggle, and Deepnote. It is based on the open-source JavaScript/C++ charting package Vizzu and allows data science professionals to leverage Python animation for data storytelling.

What is ipyvizzu? | data visualization

Ipyvizzu features a generic dataviz engine. This engine creates many types of charts and allows easy transitions between them, making it a suitable tool for creating animated data stories. The ipyvizzu-story extension allows animated charts to be presented directly from notebooks. Ipyvizzu is designed with animation in focus and follows data visualization guidelines by default. It works seamlessly with Pandas dataframes. The auto-scrolling feature keeps the chart positioned even when multiple cells run. This makes it easier to follow the data.

Installation of ipyvizzu

Let’s use Ipyvizzu in a wide variety of environments and easily install it using the pip command.

pip install ipyvizzu

The following command to upgrade it:

pip install -U ipyvizzu

Import the necessary classes and functions from the ipyvizzu library to create interactive visualizations in Jupyter notebooks:

from ipyvizzu import Chart, Data, Config, Style

Here Chart represents a visualization chart; Data represents a data object, i.e., an input to this chart; Config represents the selected chart details like chart type, x-axis, and y-axis labels, title, etc. Similarly, Style represents the style options for a chart (e.g., color, font size, line style, etc.).

Foundation of ipyvizzu

The core of an ipyvizzu chart is its animation, which includes states that define the configuration of a chart. This can be data series, labels, titles, coordinate systems, etc. Each static chart is the result of a single animation state. However, ipyvizzu can transition between multiple states automatically.

Foundation of ipyvizzu | data visualization
Source: ipyvizzu.vizzuhq.com

We can use the animate method to create a new animation state and initiate the animation, which takes both non-keyword and keyword arguments. The non-keyword arguments set the chart, while the optional keyword arguments control how ipyvizzu should animate to the new state. There are three types of non-keyword arguments:

  • data” to add the data to visualize
  • config” to adjust the chart’s series, title, geometry, alignment, etc.
  • style” to customize the chart’s appearance.

Working with Data in ipyvizzu

Currently, ipyvizzu supports two types of data series, i.e., dimensions and measures. Dimensions are the slices that ipyvizzu uses to slice the data cube, and measures are the values within the cube. Dimensions are categorical series that can contain both numbers and strings but are always considered ‘strings.’ We must include temporal data as dimensions (dates or timestamps). It is recommended to add temporal data in the order supplied in the dataset, from oldest to newest, because ipyvizzu draws elements on the chart in the same provided sequence in the dataset.

Currently, measures can only handle numerical data. There are several ways to add data to ipyvizzu, including using pandas DataFrame, defining data by series or column after column, specifying data by records or row after row, employing data cube form, and using JSON.

Here we will create a random dataset with 100 rows and 6 columns related to different products in a shop and store it in a dataframe named “data. frame”. The values in each column are generated randomly.

np.random.seed(42)
products = ['Shirt', 'Pants', 'Shoes', 'Hat']
shops = ['Shop A', 'Shop B', 'Shop C']
data_frame = pd.DataFrame({
    'Product': np.random.choice(products, 100),
    'Shop': np.random.choice(shops, 100),
    'Color': np.random.choice(['Red', 'Blue', 'Green'], 100),
    'Price': np.random.randint(10, 100, 100),
    'Quantity': np.random.randint(1, 10, 100),
    'Discount': np.random.randint(0, 50, 100)
})

Let us print the first few rows of the dataset using the following code:

head(data.frame)
 Output
Output

Next, we will create a Data object and add the dataframe to it using the add_data_frame method.

data = Data()
data.add_data_frame(data_frame)

Building Charts with ipyvizzu

Let us explore how to build different charts using ipyvizzu. To begin with, we’ll create a simple bar chart.

chart = Chart()
chart.animate(
    Config.bar(
        {"x": "Quantity",
         "y": "Product", "title": "Bar Chart"}
    )
)
Building Charts with ipyvizzu | data visualization

Here the above code initializes a Chart object and applies animation to it. Using Config. bar method, chart.animate set the chart’s configuration, which includes displaying the number of different products on the x-axis, the price of the product on the y-axis, and a title of ‘Bar Chart.’

Next, we will create a grouped bar chart.

chart.animate(
    Config.groupedBar(
        {
            "x": "Quantity",
            "y": "Color",
            "groupedBy": "Shop",
            "title": "Grouped Bar Chart",
        }
    )
)
Building Charts with ipyvizzu | data visualization

The above code creates a grouped bar chart using the Config.groupedBar method of the Config class and sets the configuration options for the chart. The groupedBy parameter determines the grouping of the bars based on a Shop column. This means that the bars are grouped by shop name, and the color of each bar represents the different colors of the products sold by that shop. The x and y variables specify the columns to be used for the x and y axes, respectively. We have also specified the title of the chart as “Grouped Bar Chart” using the title parameter. The chart with the provided parameters is shown by executing the ‘chart. animate’ method with the ‘Config.groupedBar’ configuration.

Creating Pie Chart

Now we will create a pie chart that will show the relative proportions of each product category based on the number of units sold for each product.

chart.animate(
    Config.pie(
        {
            "angle": "Quantity",
            "by": "Product",
            "title": "Pie Chart",
        }
    )
)
 Pie Chart

The above code creates a pie chart using Config. pie method of the Config class and sets the configuration options for the chart. The angle parameter determines the angle of each slice of the pie based on the “Quantity” column of data. The by parameter specifies the column to group the slices by the “Product” column. The title parameter defines the title of the chart. By calling the chart.animate method with the Config. pie configuration, the chart is displayed.

Next, we will build a Stacked Area chart. In this chart, each area shows the total price of sold products by a shop, and the areas are stacked on top of each other, indicating the total price of all products sold.

chart.animate(
    Config.stackedArea(
        {
            "x": "Product",
            "y": "Price",
            "stackedBy": "Shop",
            "title": "Stacked Area Chart",
        }
    ))
Building Charts with ipyvizzu

The above code creates a stacked area chart using the Config.stackedArea method of the Config class and sets the chart configuration options. The stackedBy parameter determines the stacking of the areas based on the “Shop” column. The x and y are the columns to be used for the x and y axes, respectively. The title parameter is used to give a title to the plot. Finally, using the chart. animate method with the Config.stackedArea configuration displays the chart with declared settings.

The style of the chart can be modified using the Style class. The Style class is used to customize the element that appears in the chart, like the color palette of markers, the font size of the chart title, etc. Here, we will use the following code to change the color palette of the markers used in the chart plot. We will set the colorPalette parameter to a list of three colors.

"colorPalette": "#0e0e52 #05668d #33A7FF"

We will again run the code to see the color change in our graph.

chart.animate(
    Style(
        {
            "plot": {
                "marker": {
                    "colorPalette": "#0e0e52 #05668d #33A7FF"
                }
            }
        }
    )
)
 Stacked Area Chart with custom colors | python animation library

Now we will animate our above charts. In the following code, we use ipyvizzu to animate four different chart types with different times and animation using ‘durations’ and ‘easing’ variables.

chart.animate(
    Config.bar(
        {"x": "Quantity",
         "y": "Product", "title": "Bar Chart"}
    ),
    duration=1,
    easing="linear"
)
chart.animate(
    Config.groupedBar(
        {
            "x": "Quantity",
            "y": "Color",
            "groupedBy": "Shop",
            "title": "Grouped Bar Chart",
        }
    ),
    duration=6,
    easing="linear"
)
chart.animate(
    Config.pie(
        {
            "angle": "Quantity",
            "by": "Product",
            "title": "Pie Chart",
        }
    ),
    duration=4,
    easing="linear"
)
chart.animate(
    Config.stackedArea(
        {
            "x": "Product",
            "y": "Price",
            "stackedBy": "Shop",
            "title": "Stacked Area Chart",
        }
    ),
    duration=4,
    easing="linear"
)
python animation library | data visualisation

The Chart object is created and then animated using the Config. bar, Config.groupedBar, Config.pie, Config.stackedArea methods, and configurations.

You can find Ipyvizzu on app platforms like Flask, Panel, Streamlit, etc., making integrating into your existing projects and workflows easy.  As a result, ipyvizzu is a great and flexible choice for data visualization in Python. This tutorial’s code with animation is available on my GitHub repository.

Possible Applications of ipyvizzu

Next, let us take a look at two possible real-world applications of ipyvizzu:

  1. Healthcare Data Analysis: We know that healthcare datasets are often large and complex, and data visualizations can assist healthcare professionals in gaining insights into patient health and diagnosis outlook. Ipyvizzu can help create interactive visualizations for healthcare data analysis (e.g., patient health status, treatment effectiveness, disease prevalence, etc.). The ability to interact with visualizations can help healthcare professionals identify patterns and make data-driven decisions.
  2. Financial Data Analysis: Financial datasets are quite challenging to analyze. Analysts often use complex visualization techniques to understand the data. We can use ipyvizzu to create interactive and animated visualizations for financial data analysis, including stock market trends, trading volumes, and investment portfolios, which will help explore different scenarios. This makes it possible to make better decisions for financial gain.

Other than the above two, interactive data visualizations using ipyvizzu can be beneficial in marketing campaigns where marketers can gain insights into customer behavior and preferences with marketing data (e.g., website traffic, email click rates, and social media engagement) and improve their ROI (return on investment).

Conclusion

In conclusion, ipyvizzu is a great package for creating animated charts and telling stories with data using Python.

The key takeaways from this tool are:

  • The ipyvizzu package is a simple and easy-to-use Python package that can build animated stories.
  • It is an animated charting tool for Jupyter, Google Colab, Databricks, Kaggle, and Deepnote notebooks.
  • Several ways are available to add data to ipyvizzu for building visualizations.
  • A variety of plots can be created with ipyvizzu.
  • It is possible to animate multiple charts with ipyvizzu.

To explore ipyvizzu, I recommend reading the official documentation of the library for sample code and inspiration. Try this library to create interactive visualizations for your next exploratory data analysis.

The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers

  • [tta_listen_btn class="listen"]