Reader small image

You're reading from  Interactive Data Visualization with Python - Second Edition

Product typeBook
Published inApr 2020
Reading LevelIntermediate
Publisher
ISBN-139781800200944
Edition2nd Edition
Languages
Right arrow
Authors (4):
Abha Belorkar
Abha Belorkar
author image
Abha Belorkar

Abha Belorkar is an educator and researcher in computer science. She received her bachelor's degree in computer science from Birla Institute of Technology and Science Pilani, India and her Ph.D. from the National University of Singapore. Her current research work involves the development of methods powered by statistics, machine learning, and data visualization techniques to derive insights from heterogeneous genomics data on neurodegenerative diseases.
Read more about Abha Belorkar

Sharath Chandra Guntuku
Sharath Chandra Guntuku
author image
Sharath Chandra Guntuku

Sharath Chandra Guntuku is a researcher in natural language processing and multimedia computing. He received his bachelor's degree in computer science from Birla Institute of Technology and Science, Pilani, India and his Ph.D. from Nanyang Technological University, Singapore. His research aims to leverage large-scale social media image and text data to model social health outcomes and psychological traits. He uses machine learning, statistical analysis, natural language processing, and computer vision to answer questions pertaining to health and psychology in individuals and communities.
Read more about Sharath Chandra Guntuku

Shubhangi Hora
Shubhangi Hora
author image
Shubhangi Hora

Shubhangi Hora is a data scientist, Python developer, and published writer. With a background in computer science and psychology, she is particularly passionate about healthcare-related AI, including mental health. Shubhangi is also a trained musician.
Read more about Shubhangi Hora

Anshu Kumar
Anshu Kumar
author image
Anshu Kumar

Anshu Kumar is a data scientist with over 5 years of experience in solving complex problems in natural language processing and recommendation systems. He has an M.Tech. from IIT Madras in computer science. He is also a mentor at SpringBoard. His current interests are building semantic search, text summarization, and content recommendations for large-scale multilingual datasets.
Read more about Anshu Kumar

View More author details
Right arrow

4. Interactive Visualization of Data across Strata

Learning Objectives

By the end of this chapter, you will be able to:

  • Create interactivity in scatter plots using altair
  • Use zoom in and out, hover and tooltip, and select and highlight on scatter plots
  • Create interactive bar plots and heatmaps
  • Create dynamic links between different types of plots within a single rich interactive visualization

In this chapter, you will learn to create interactive visualizations for data stratified with respect to any categorical variable.

Introduction

In the previous chapters, we went through a variety of techniques for visualizing data effectively based on the type of features in the dataset and learned how to introduce interactivity in plots using the plotly library. The second section of this book, starting with this chapter, will guide you on building interactive visualizations with Python for a variety of contexts. An observation made in the previous chapter was that when it comes to introducing interactivity in certain types of Python plots, plotly can sometimes be verbose, and may involve a steep learning curve. Therefore, in this chapter, we'll introduce altair, a library designed especially for generating interactive plots. We will demonstrate how to create interactive visualizations with altair for data stratified with respect to any categorical variable. For illustration, we will use a publicly available dataset to generate scatter plots and bar plots with the features in the dataset and add a variety...

Interactive Scatter Plots

As you know by now, scatter plots are one of the most essential types of plots for presenting global patterns within a dataset. Naturally, it is important to know how to introduce interactivity in these plots. We will first look at the zoom and reset actions on plots. Before that, though, let's have a look at the dataset.

We can view the HPI dataset using the following code:

import pandas as pd
#Download the data from Github repo 
hpi_url = "https://raw.githubusercontent.com/TrainingByPackt/Interactive-Data-Visualization-with-Python/master/datasets/hpi_data_countries.tsv"
# Once downloaded, read it into a DataFrame using pandas
hpi_df = pd.read_csv(hpi_url, sep='\t')
hpi_df.head()

The output is as follows:

Figure 4.1: HPI dataset

Note that there are 5 numerical/quantitative features in this dataset: Life Expectancy (years)Wellbeing (0-10)Inequality of outcomesEcological Footprint...

Other Interactive Plots in altair

Now that we know how to add interactivity to scatter plots, let's learn how to introduce interactivity to two other important visualization types – bar plots and heatmaps. We also encourage you to read the official documentation and look at the official example gallery at https://altair-viz.github.io/gallery/index.html to explore altair so as to be aware of the wide variety of visualization types possible in it.

Exercise 33: Adding a Zoom-In and Zoom-Out Feature and Calculating the Mean on a Static Bar Plot

In this exercise, first, we will generate a simple (static) bar plot and then explore interactivity such as zooming in and out. Then, we'll use the same bar plot and find out the mean of the Happy Planet Index of each region. We'll use the altair library here and the Happy Planet Index dataset:

  1. Import the altair module as alt:
    import altair as alt
  2. Read from the dataset:
    hpi_url = "https://raw.githubusercontent...

Summary

In this chapter, we learned how to create visualizations that respond to the selection of specific strata in a dataset. For illustration purposes, we used the Happy Planet Index dataset of 140 countries, creating a variety of plots with stratification based on the different regions to which countries belonged. We generated scatter plots, bar plots, and heatmaps with interactive features such as zooming in and out, tool tipping, the selection of datapoints in a user-specified interval, and the selection of datapoints belonging to specific strata. We also generated more complex visualizations with multiple plots interlinked with each other that dynamically respond to user inputs. In the next chapter, we will learn how to create interactive visualizations of data across time. 

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Interactive Data Visualization with Python - Second Edition
Published in: Apr 2020Publisher: ISBN-13: 9781800200944
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (4)

author image
Abha Belorkar

Abha Belorkar is an educator and researcher in computer science. She received her bachelor's degree in computer science from Birla Institute of Technology and Science Pilani, India and her Ph.D. from the National University of Singapore. Her current research work involves the development of methods powered by statistics, machine learning, and data visualization techniques to derive insights from heterogeneous genomics data on neurodegenerative diseases.
Read more about Abha Belorkar

author image
Sharath Chandra Guntuku

Sharath Chandra Guntuku is a researcher in natural language processing and multimedia computing. He received his bachelor's degree in computer science from Birla Institute of Technology and Science, Pilani, India and his Ph.D. from Nanyang Technological University, Singapore. His research aims to leverage large-scale social media image and text data to model social health outcomes and psychological traits. He uses machine learning, statistical analysis, natural language processing, and computer vision to answer questions pertaining to health and psychology in individuals and communities.
Read more about Sharath Chandra Guntuku

author image
Shubhangi Hora

Shubhangi Hora is a data scientist, Python developer, and published writer. With a background in computer science and psychology, she is particularly passionate about healthcare-related AI, including mental health. Shubhangi is also a trained musician.
Read more about Shubhangi Hora

author image
Anshu Kumar

Anshu Kumar is a data scientist with over 5 years of experience in solving complex problems in natural language processing and recommendation systems. He has an M.Tech. from IIT Madras in computer science. He is also a mentor at SpringBoard. His current interests are building semantic search, text summarization, and content recommendations for large-scale multilingual datasets.
Read more about Anshu Kumar