2.3 Comparing Plotting Libraries and Declarative Visualizations

2.3 Comparing Plotting Libraries and Declarative Visualizations#

from plotnine import *
from matplotlib import pyplot as plt
from plotnine import data
import plotly.express as px
import seaborn as sns
mpg = data.mpg

Bar Chart#

mpg
manufacturer model displ year cyl trans drv cty hwy fl class
0 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
1 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
2 audi a4 2.0 2008 4 manual(m6) f 20 31 p compact
3 audi a4 2.0 2008 4 auto(av) f 21 30 p compact
4 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
... ... ... ... ... ... ... ... ... ... ... ...
229 volkswagen passat 2.0 2008 4 auto(s6) f 19 28 p midsize
230 volkswagen passat 2.0 2008 4 manual(m6) f 21 29 p midsize
231 volkswagen passat 2.8 1999 6 auto(l5) f 16 26 p midsize
232 volkswagen passat 2.8 1999 6 manual(m5) f 18 26 p midsize
233 volkswagen passat 3.6 2008 6 auto(s6) f 17 26 p midsize

234 rows × 11 columns

# Pandas
(mpg['manufacturer']
 .value_counts(sort=False)
 .plot.barh()
 .set_title('Number of Cars by Make')
)
Text(0.5, 1.0, 'Number of Cars by Make')
../_images/54902fd917d08a2db32905badad0497b51598fbc039c05a50d4be9f78b94b065.png
# Plotnine (ggplot2 clone)
(ggplot(mpg) + 
   aes(x='manufacturer') +
   geom_bar() + 
   coord_flip() +
   ggtitle('Number of Cars by Make')
)
fig = px.bar(
    mpg.groupby('manufacturer', observed=False).size().reset_index(name='count'),
    x='count',
    y='manufacturer',
    orientation='h',
    title='Number of Cars by Make',
)

fig

Scatter Plot#

(mpg.
    plot.
    scatter(x='displ', y='hwy'))
<Axes: xlabel='displ', ylabel='hwy'>
../_images/6695e2352ae759a314d44b40cf9d770092cedea11131dc639f6c48007910dfb6.png
mpg
manufacturer model displ year cyl trans drv cty hwy fl class
0 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
1 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
2 audi a4 2.0 2008 4 manual(m6) f 20 31 p compact
3 audi a4 2.0 2008 4 auto(av) f 21 30 p compact
4 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
... ... ... ... ... ... ... ... ... ... ... ...
229 volkswagen passat 2.0 2008 4 auto(s6) f 19 28 p midsize
230 volkswagen passat 2.0 2008 4 manual(m6) f 21 29 p midsize
231 volkswagen passat 2.8 1999 6 auto(l5) f 16 26 p midsize
232 volkswagen passat 2.8 1999 6 manual(m5) f 18 26 p midsize
233 volkswagen passat 3.6 2008 6 auto(s6) f 17 26 p midsize

234 rows × 11 columns

(mpg
 .plot
 .scatter(x='displ', y='hwy')
 .set(title='Engine Displacement in Liters vs Highway MPG',
      xlabel='Engine Displacement in Liters',
      ylabel='Highway MPG'));
../_images/ed6a3c3c677e09ff8842004f98cbb2f0439100555548e0f8f7158f43a75732a5.png
(ggplot(mpg) +
    aes(x = 'displ', y = 'hwy') +
    geom_point() + 
    ggtitle('Engine Displacement in Liters vs Highway MPG') +
    xlab('Engine Displacement in Liters') +
    ylab('Highway MPG')
)
fig = px.scatter(
    mpg,
    x='displ',
    y='hwy',
    title='Engine Displacement in Liters vs Highway MPG',
    labels={
        'displ': 'Engine Displacement in Liters',
        'hwy': 'Highway MPG'
    }
)

fig.show()

Scatter Plot, Faceted with Color#

for c, df in mpg.groupby('class'):
    plt.scatter(df['displ'], df['hwy'], label=c)
../_images/73fbbfb74668a4b7a3c0dc9ee89694693b82fcbf4d897ea58a4082c728f00d7c.png
fig, ax = plt.subplots()
for c, df in mpg.groupby('class'):
    plt.scatter(df['displ'], df['hwy'], label=c)
../_images/73fbbfb74668a4b7a3c0dc9ee89694693b82fcbf4d897ea58a4082c728f00d7c.png
fig, ax = plt.subplots()
for c, df in mpg.groupby('class'):
    ax.scatter(df['displ'], df['hwy'], label=c)
ax.legend()
ax.set_title('Engine Displacement in Liters vs Highway MPG')
ax.set_xlabel('Engine Displacement in Liters')
ax.set_ylabel('Highway MPG')
Text(0, 0.5, 'Highway MPG')
../_images/8005d09ebd8425ec1a6277b36fa3ee4ddc6dd1a1274ec5d238a53e10d29220f3.png
(sns
 .FacetGrid(mpg, hue='class', height=5)
 .map(plt.scatter, 'displ', 'hwy')
 .add_legend()
 .set(
    title='Engine Displacement in Liters vs Highway MPG',
    xlabel='Engine Displacement in Liters',
    ylabel='Highway MPG'
))
<seaborn.axisgrid.FacetGrid at 0x3233bbe50>
../_images/8bf394be028625f33e75284cc95e475971e7a88982daf66c184f72b324b797c1.png
(ggplot(mpg) + 
    aes(x = 'displ', y = 'hwy', color = 'class') +
    geom_point() + 
    ggtitle('Engine Displacement in Liters vs Highway MPG') +
    xlab('Engine Displacement in Liters') +
    ylab('Highway MPG'))
fig = px.scatter(
    mpg,
    x='displ',
    y='hwy',
    color='class',
    title='Engine Displacement in Liters vs Highway MPG',
    labels={
        'displ': 'Engine Displacement in Liters',
        'hwy': 'Highway MPG',
        'class': 'Vehicle Class'
    }
)

fig.show()