Posts about plotly

Visualization Samples by Plotly Express

Goal

This post aims to introduce examples of visualization by Plotly Express.

The followings are introduced:

  • Prepared example data
  • Scatter plot
    • basic
    • basic + size
    • basic + size + color
    • basic + size + color + time
    • heatmap + histogram

2019-07-19 21-54-58 2019-07-19 21_56_00_gapminder_plotly_express

Reference

Libraries

In [8]:
import pandas as pd
import numpy as np
import plotly_express as px
import plotly.io as pio
pio.renderers.default = "png"

Load a prepared data

Car Share

In [2]:
df = px.data.carshare()
df.head()
Out[2]:
centroid_lat centroid_lon car_hours peak_hour
0 45.471549 -73.588684 1772.750000 2
1 45.543865 -73.562456 986.333333 23
2 45.487640 -73.642767 354.750000 20
3 45.522870 -73.595677 560.166667 23
4 45.453971 -73.738946 2836.666667 19

Tips

In [3]:
df = px.data.tips()
df.head()
Out[3]:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4

Election

In [4]:
df = px.data.election()
df.head()
Out[4]:
district Coderre Bergeron Joly total winner result
0 101-Bois-de-Liesse 2481 1829 3024 7334 Joly plurality
1 102-Cap-Saint-Jacques 2525 1163 2675 6363 Joly plurality
2 11-Sault-au-Récollet 3348 2770 2532 8650 Coderre plurality
3 111-Mile-End 1734 4782 2514 9030 Bergeron majority
4 112-DeLorimier 1770 5933 3044 10747 Bergeron majority

Wind

In [5]:
df = px.data.wind()
df.head()
Out[5]:
direction strength frequency
0 N 0-1 0.5
1 NNE 0-1 0.6
2 NE 0-1 0.5
3 ENE 0-1 0.4
4 E 0-1 0.4

Gap Minder

In [6]:
df = px.data.gapminder()
df.head()
Out[6]:
country continent year lifeExp pop gdpPercap iso_alpha iso_num
0 Afghanistan Asia 1952 28.801 8425333 779.445314 AFG 4
1 Afghanistan Asia 1957 30.332 9240934 820.853030 AFG 4
2 Afghanistan Asia 1962 31.997 10267083 853.100710 AFG 4
3 Afghanistan Asia 1967 34.020 11537966 836.197138 AFG 4
4 Afghanistan Asia 1972 36.088 13079460 739.981106 AFG 4

Scatter Plot

Basic Scatter plot

In [10]:
px.scatter(df, x='gdpPercap', y='lifeExp', width=900, height=400)

Scatter plot + size

In [11]:
px.scatter(df, x='gdpPercap', y='lifeExp', size='pop', width=900, height=400)

Scatter plot + size + color

In [12]:
px.scatter(df, x='gdpPercap', y='lifeExp', size='pop', color='country', width=900, height=400)

Scatter plot + size + color + time

In [13]:
px.scatter(df, x='gdpPercap', y='lifeExp', size='pop', color='country', animation_frame='year', width=900, height=400)
In [14]:
px.density_heatmap(df, x="gdpPercap", y="lifeExp", marginal_y="histogram", marginal_x="histogram")