Home
  • Portfolio
  • Tableau
  1. Reports
  2. Plotting with ggplot2 in Python
  • Reports
    • BI with SQL & Markdown
    • BI as Code
    • Docs as Code
    • Slides as Code
    • Duckdb & Evidence
    • Duckdb & Google Sheets
    • Judiciary Elections in Mexico
    • Social Security in Mexico
    • National Guard in Mexico
    • The Economist & Data Scientist Role in Public Security
    • Plotting with ggplot2 in Python
    • Understanding Spotify User Behavior
    • Gasoline Prices in Mexico
    • Farmacias Similares Locations
    • Cluster analysis with Python and Polars
  • Slideshows
    • Gasoline Prices in Mexico
    • Water Collection Systems in Mexico
    • Goodbye, Lake Zumpango
    • Why BI tools Fall short
    • Window Functions in SQL
    • Dashboards vs Web reports
  • Dashboards
    • Gasoline Prices in Mexico
    • Farmacias Similares Dashboard
    • Mexico City Crime Dashboard
    • Streamlit Financial Dashboard
    • Percepción de Seguridad en México ENSU 2015-2025
    • Car Sales Dashboard

Table of Contents

  • Overview
  • A Layered Grammar of Graphics
  • Environment settings
  • Dataset
  • Contact

Other Links

  • My Portfolio
  1. Reports
  2. Plotting with ggplot2 in Python

ggplot2 in Python

  • Show All Code
  • Hide All Code

An Overview of the famous ggplot2 R package in Python

Author

Jesus LM
Economist & Data Scientist

Published

Nov, 2023

Abstract

Plotnine library in Python is based on the famous ggplot2 R package. In this article, we will show some examples of this superb visualization library.

Overview

ggplot plotnine

ggplot2 is based on Hadley Wickham’s Layered Grammar of Graphics1, which provides a systematic way to describe the components of a statistical graphic.

ggplot2 is a declarative language, which implies to specify the “what you want” to plot, rather than “the how”.

ggplot2 has a wide range of features, including:

  • Support for a variety of data types (data frames, matrices, and lists)
  • A wide variety of geometric objects (points, lines, bars, and histograms)
  • A variety of statistical transformations (aggregation, smoothing, and binning)
  • A powerful system for customizing plots

A Layered Grammar of Graphics

There are 03 compulsory components for building visualizations with plotnine:

  1. Data
  2. Aesthetics
  3. Geometric objects

Environment settings

Show code
from plotnine import *
from plotnine.data import *
import numpy as np
import pandas as pd
import polars as pl
from datetime import datetime
import warnings
warnings.filterwarnings('ignore')

Dataset

Show code
economics = pl.from_pandas(economics)
Show code
(
    ggplot(data=economics)
    + aes(x='date', y='uempmed')
    + geom_line(color='#670000', size=1)
    + labs(title='US unemployed duration (weeks)')
    + theme(figure_size=(16, 8),
            axis_text_x = element_text(color="black", size=18, angle=0, hjust=.3),
            axis_text_y = element_text(color="grey", size=16), 
            plot_title = element_text(size = 25, face = "bold"), 
            axis_title = element_text(size = 18)
            )
    + xlab('')
    + ylab('Unemployed duration (median)')
).draw()

Contact

Jesus LM
Economist & Data Scientist

Linkedin | Medium | Twitter

Footnotes

  1. Hadley Wickham (2010) A layer Grammar of Graphics.↩︎

The Economist & Data Scientist Role in Public Security
Understanding Spotify User Behavior
 
  • License

  •   © 2026