Plotting in Python

Author

Marie-Hélène Burle

There are many packages that provide plotting in Python. Here is an overview of the most popular ones.

matplotlib

matplotlib is a very popular Python plotting library because it provides full control over the plots and produces graphs well-suited for publications. Many plot types can be created.

The downside of having full control is that it has a verbose imperative syntax. It also produces non-interactive plots.

Example

Here is one of the matplotlib website examples to give you an idea of the syntax:

import matplotlib.pyplot as plt
import numpy as np

# make data:
np.random.seed(1)
x = np.random.uniform(-3, 3, 256)
y = np.random.uniform(-3, 3, 256)
z = (1 - x/2 + x**5 + y**3) * np.exp(-x**2 - y**2)
levels = np.linspace(z.min(), z.max(), 7)

# plot:
fig, ax = plt.subplots()

ax.plot(x, y, 'o', markersize=2, color='grey')
ax.tricontourf(x, y, z, levels=levels)

ax.set(xlim=(-3, 3), ylim=(-3, 3))

plt.show()

seaborn

Seaborn is a higher level library built on top of matplotlib. This means that the plots are also non-interactive and that the options are more limited. On the plus side, the declarative syntax is very easy, making it a nice library for exploratory data analysis (EDA).

Example

Here is one example from the seaborn gallery:

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="dark")

# Simulate data from a bivariate Gaussian
n = 10000
mean = [0, 0]
cov = [(2, .4), (.4, .2)]
rng = np.random.RandomState(0)
x, y = rng.multivariate_normal(mean, cov, n).T

# Draw a combo histogram and scatterplot with density contours
f, ax = plt.subplots(figsize=(6, 6))
sns.scatterplot(x=x, y=y, s=5, color=".15")
sns.histplot(x=x, y=y, bins=50, pthresh=.1, cmap="mako")
sns.kdeplot(x=x, y=y, levels=5, color="w", linewidths=1)

Website

bokeh

Bokeh creates interactive plots great for dashboards and web apps. It is more efficient than other libraries for streaming or interactions with large datasets. It does have a fairly steep learning curve.

Website

plotly

Plotly also creates interactive plots and its declarative option with the Plotly Express framework makes it very easy to use (the imperative approach is also possible). Static plots however are less sophisticated than with matplotlib and it is not as good as Bokeh for dashboard interactions.

Example

import plotly.express as px
df = px.data.tips()

fig = px.density_contour(df, x="total_bill", y="tip")
fig.update_traces(contours_coloring="fill", contours_showlabels = True)
fig.show()
510152025010203040500246810
051015202530total_billtip

Website

Vega-Altair

Vega-Altair is a declarative library with an easy syntax and interactive plots options, ideal for EDA.

Example

import altair as alt
from vega_datasets import data

source = data.us_employment()

predicate = alt.datum.nonfarm_change > 0
color = alt.when(predicate).then(alt.value("steelblue")).otherwise(alt.value("orange"))

alt.Chart(source).mark_bar().encode(
    x="month:T",
    y="nonfarm_change:Q",
    color=color
).properties(width=600)

Website

plotnine

Plotnine is an adaptation to Python of the popular R library ggplot2 based on the grammar of graphics concept.

Example

from plotnine import (
    ggplot,
    aes,
    theme_matplotlib,
    theme_set,
    geom_tile,
    scale_fill_continuous,
    coord_cartesian
)

from plotnine.data import faithfuld

# Set default theme for all the plots
theme_set(theme_matplotlib())

(
    ggplot(faithfuld, aes("waiting", "eruptions", fill="density")) 
    + geom_tile()
)

Website

Summary

Library Syntax Interactivity Ideal for Weaknesses
Matplotlib Imperative No Publications Verbose, non-interactive
Seaborn Declarative No Statistical EDA Limited for rare plot types
Bokeh Imperative Yes Live web apps Styling is less intuitive
Plotly Both Yes Dashboards Static output less flexible
Altair Declarative Yes Fast EDA, web Fewer custom options
Plotnine Declarative No R users Somewhat niche