Interactive Visualization in Python

AbdulMajedRaja RS

Outline

  • Why Interactive Visualization?
  • Plotly Express - Intro
  • Basic Visualizations
  • Improving a Plot - One Component at a time
  • Building a Story - with one line of Code

Why Interactive Visualization?

In [1]:
import seaborn as sns
import matplotlib.pyplot as plt
crashes = sns.load_dataset("car_crashes")
sns.set(rc={'figure.figsize':(11.7,8.27)})

Static Visualization - Find the Values

In [2]:
sns.barplot(x = 'speeding', y = 'abbrev', data = crashes);

Interactive Visualization - Just, Hover over!

In [3]:
import plotly_express as px
px.bar(data_frame = crashes, x = 'speeding', y = 'abbrev', color = 'abbrev', orientation= "h")

Interactive over Static

  • Intuitive - More Readable
  • Less Code, More Stories/Insights
  • Customizable - Pan, Zoom, Select
  • This is 2019!!!

Plotly Express

Plotly Express

  • High-level Python visualization library
  • Wrapper around plotly.py
  • Plotly Express is totally free
  • Right now, Only available for Python
  • A simple syntax for complex charts
  • Inspired by Seaborn and ggplot2 (R)
  • Consisten & Easy-to-learn API
  • Offline-mode by Default

Installation

In [4]:
# !! pip3 install plotly_express # Python3

Loading

In [5]:
import plotly_express as px

Dataset

In [6]:
gapminder = px.data.gapminder()

gapminder.tail(4)
Out[6]:
country continent year lifeExp pop gdpPercap iso_alpha iso_num
1700 Zimbabwe Africa 1992 60.377 10704340 693.420786 ZWE 716
1701 Zimbabwe Africa 1997 46.809 11404948 792.449960 ZWE 716
1702 Zimbabwe Africa 2002 39.989 11926563 672.038623 ZWE 716
1703 Zimbabwe Africa 2007 43.487 12311143 469.709298 ZWE 716

Simple Visualizations

Bar

In [7]:
px.bar(data_frame= gapminder[gapminder.year==2007], x = 'country', y = 'pop')

Histogram

In [8]:
px.histogram(data_frame= gapminder, x = 'pop')

Box plot - Normal

In [9]:
px.box(data_frame= gapminder, y = 'lifeExp')

Box Plot - Colored / Grouped

In [10]:
px.box(data_frame= gapminder, y = 'lifeExp', color = 'continent')

Violin plot

In [11]:
px.violin(data_frame= gapminder, y = 'lifeExp', color = 'continent', box=True)

Line Graph

In [12]:
px.line(gapminder.query("country == ['India','China','United States']"), 'year','pop', color = 'country')

Improving a Plot - One Component at a Time

Scatter Plot

In [13]:
px.scatter(data_frame= gapminder, x = 'gdpPercap', y = 'lifeExp')

Scatter Plot with Box and Histogram

In [14]:
px.scatter(data_frame= gapminder, x = 'gdpPercap', y = 'lifeExp', marginal_y='box', marginal_x='histogram')

Scatter Plot with Trend Line

In [15]:
px.scatter(data_frame= gapminder, x = 'gdpPercap', y = 'lifeExp', trendline='ols')

Scatter Plot with Country Values (Hover)

In [16]:
px.scatter(data_frame= gapminder, x = 'gdpPercap', y = 'lifeExp', hover_name = 'country', trendline= 'ols')

Scatter Plot with Country Values (Hover) & Color - Transformed Axis

In [17]:
px.scatter(data_frame= gapminder, x = 'gdpPercap', y = 'lifeExp', hover_name = 'country', color = 'country', log_x=True)

Building a Story for 2017

With Just one Line of Code

In [18]:
gapminder7 = gapminder[gapminder.year==2007]

Scatter Plot

In [19]:
px.scatter(data_frame= gapminder7, x = 'gdpPercap', y = 'lifeExp', hover_name = 'country', 
           log_x=True)

Scatter Plot - Transformed Axis & Facet - with Trendline

In [20]:
px.scatter(data_frame= gapminder7, x = 'gdpPercap', y = 'lifeExp', hover_name = 'country', 
           log_x=True, facet_col="continent", trendline = 'ols')

Bubble Chart

In [21]:
px.scatter(data_frame= gapminder7, x = 'gdpPercap', y = 'lifeExp', hover_name = 'country', 
           log_x=True, #facet_col="continent", 
           color = 'continent', size = 'pop')

Bubble Chart with Facet by Continent

In [22]:
px.scatter(data_frame= gapminder7, x = 'gdpPercap', y = 'lifeExp', hover_name = 'country', 
           log_x=True, facet_col="continent", 
           color = 'continent', size = 'pop')

Scatter Plot with Country Values (Hover) & Color

In [23]:
px.scatter(data_frame= gapminder7, x = 'gdpPercap', y = 'lifeExp', hover_name = 'country', color = 'country')
In [24]:
px.scatter(data_frame= gapminder7, x = 'gdpPercap', y = 'lifeExp', hover_name = 'country', color = 'country', 
           size = 'pop')

The Big Picture - Timeline Animation

In [25]:
px.scatter(px.data.gapminder(), x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
           size="pop", color="country", hover_name="country", 
           log_x = True, 
           size_max=45, range_x=[100,100000], range_y=[25,90])

Thank you