Generate Interactive Plots in one line of Python Code
By: Satyam Kumar
Tags: Artificial Intelligence, Data Science, Education, Machine Learning, Exploratory Data Analysis

Generate Interactive Plots in one line of Python Code

Image by Colin Behrens from Pixabay

Exploratory data analysis is an essential component of a data science model development pipeline. A data scientist spends most of the time performing EDA, to get a better understanding and generating insights from the data.

There are various univariate, bivariate, and multivariate visualizing techniques to perform EDA. Matplotlib, Seaborn are some of the popular libraries to generate static plots and charts. The plots generated using seaborn and matplotlib are static in nature and require multiple lines of Python code to further customize the plots.

In this article, we will explore the Plotly Express library, which can be used to generate interactive plots.

Why Interactive Visualization?

Matplotlib and Seaborn are popular libraries among the data science community that generate beautiful plots and charts for visualization, so what’s the requirement of Interactive Visualization.

A data scientist has to spend a lot of time to generate and customize the plots using the seaborn or matplotlib library.

Let's generate a sample static barplot using seaborn to visualize the population of several countries in the year 2007.

sns.barplot(data=df, x='country', y='pop')

plt.show()

(Image by Author), Seaborn Bar Plot

The above sample bar plot showcasing the population of each country is not readable. A data scientist has to write multiple lines of Python code to customize the bar plot to make it understandable. One needs to resize the plot and rotate the x-ticks labels.

plt.figure(figsize=(14,4))

sns.barplot(data=df, x='country', y='pop')

plt.xticks(rotation=90)

plt.grid(axis='y')

plt.show()

(Image by Author), Customize Seaborn Bar Plot

There are 142 categories (Countries) in the X-axis, which makes the plot difficult to interpret. One must filter the top 60 countries for better visualization.

plt.figure(figsize=(14,4))

sns.barplot(data=df[df['country'].isin(list(df['country'].unique())[:60])], x='country', y='pop')

plt.xticks(rotation=90)

plt.grid(axis='y')

plt.show()

(Image by Author), Customize Seaborn Bar Plot for top 50 categories

One must write multiple lines of Python code, to make the plot beautiful and intuitive. Still, the plots are static in nature, and it's a bit difficult to get the exact population of each country from the plot.

Plotly Express is an efficient library that can generate beautiful and interpretable plots and charts in a single line of Python code. Now let’s generate the same plot using the Plotly Express library.

Plotly Express:

From Plotly Documentation: Plotly Express is a wrapper around the plotly library. The function APIs for Plotly Express is designed to be as consistent and easy to learn as possible, making it easy to switch from scatter plots to a bar chart to a histogram to a sunburst chart throughout a data exploration session.

Plotly Express generates intuitive and interactive plots in a single line of Python code.

Installation:

Plotly Express can be installed from PyPI using

pip install plotly

Usage:

To generate the interactive bar plot using Plotly Express literally takes a single line of Python code.

import plotly.express as px px.bar(data_frame=df, x='country', y='pop')

(GIF by Author), Bar plot using Plotly Express

Plotly Express provides more than 30 functions for creating different types of plots and figures. Read the library documentation to get a better understanding of code usage.