Plotnine: ggplot in python


Plotnine is a library that allows to use the grammar of graphics in Python. It is based on the famous ggplot2 library in R.

Plotnine is a great tool to create beautiful and complex visualizations with a syntax that R users already know and love.

⏱ Quick start

Before using plotnine you need to install it. This can easily be done with pip:


pip install plotnine

# Load plotnine
from plotnine import ggplot, geom_point, aes
import pandas as pd

# Sample data
x = [1,2,3,4,5,6,7,8,9,10]
y = [10,9,8,7,6,5,4,3,2,1]
df = pd.DataFrame({'x':x, 'y':y})

# Create a chart
(
ggplot(df, aes(x='x', y='y')) +
    geom_point()
)

Scatter plots are a great way to visualize the relationship between two numerical variables. The plotnine library makes it easy to thanks to its geom_point() function.


Bar plots are a great way to visualize the relationship between a categorical variable and a numerical one. The plotnine library makes it easy thanks to its geom_bar() function.

The following examples show how to create a basic bar plot with plotnine and how to customize it.


Bar plots are a great way to visualize the distribution of a numerical variable. The plotnine library makes it easy thanks to its geom_histogram() function.

The following examples show how to create a basic histogram with plotnine and how to customize it.


Plotnine allows to change the theme of the chart. This can easily be done by adding theme_* functions to the chart.


Contact


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥