← Back to list

ggplot2 for Python: Plotnine

A scatter plot case study

Jesus LM in T3CH · 2025-01-08 01:42 · 54 claps · 3.1 min read
#data-science #python #plotnine #ggplot2
Open on Medium ↗
Wiki topics: ML · Machine Learning 🔬 · Science · General

ggplot2 for Python: Plotnine

A scatter plot case study

https://posit.co/blog/improving-the-layout-of-plotnine-graphics/

https://posit.co/blog/improving-the-layout-of-plotnine-graphics/

Contents

· Overview · Key Features ∘ Grammar-Based ∘ Flexibility ∘ Data-Driven ∘ Integration ∘ Extensible · Components · Conclusions · References · Contact

Overview

Plotnine is a Python library for creating elegant and informative statistical graphics based on the Grammar of Graphics, a conceptual framework developed by Leland Wilkinson. This framework provides a structured way to compose plots by combining data, geometric objects (points, lines, bars, etc.), and aesthetic mappings (color, size, shape).

Key Features

Grammar-Based

Plotnine adheres to the Grammar of Graphics, making it intuitive to understand and compose complex plots by combining layers of data and visual elements.

Flexibility

It offers a wide range of customizable options for fine-tuning plot aesthetics, including colors, shapes, labels, and themes.

Data-Driven

Plotnine excels at visualizing various data types, from simple scatter plots to complex multi-panel figures.

Integration

It seamlessly integrates with pandas DataFrames, making it easy to work with and visualize tabular data.

Extensibility

The library can be extended with custom geoms, stats, and scales to create unique and specialized visualizations.

Components

  • Data: The dataset containing the information to be visualized.
  • Geoms: Geometric objects (points, lines, bars, etc).
  • Aesthetics: Visual properties (color, size, shape) mapped to data variables.
  • Scales: Functions that translate data values into visual properties.
  • Facets: Subdivide the plot into multiple panels based on data variables.
  • Coordinates: Control the coordinate system (Cartesian, polar, etc.).
  • Theme: Define the overall visual style of the plot (fonts, colors, background).

Environment settings

# import libraries
from plotnine import *
from plotnine.data import *
import numpy as np
import polars as pl

Read dataset

# read plotnine dataset
mpg = pl.from_pandas(mpg)

Charts

(
    ggplot(data=mpg)
    + geom_point(mapping=aes(x="displ", y="hwy"))
).draw()

scatter plot between engine size and fuel efficiency

scatter plot between engine size and fuel efficiency

The plot shows a negative relationship between engine size (displ) and fuel efficiency (hwy). In other words, cars with big engines use more fuel.

(
    ggplot(data=mpg)
    + geom_point(mapping=aes(x='cyl', y='hwy'))
).draw()

scatter plot by cylinder

scatter plot by cylinder

(
    ggplot(data=mpg)
    + geom_point(mapping=aes(x="displ", y="hwy", color="class"))
).draw()

scatter plot by class color

scatter plot by class color

(
    ggplot(data=mpg)
    + geom_point(mapping=aes(x="displ", y="hwy", size="class"))
).draw()

scatter plot by class size

scatter plot by class size

(
    ggplot(data=mpg)
    + geom_point(mapping=aes(x="displ", y="hwy", alpha="manufacturer"))
).draw()

scatter plot by manufacturer transparency

scatter plot by manufacturer transparency

(
    ggplot(data=mpg)
    + geom_point(mapping=aes(x="displ", y="hwy", shape="manufacturer"))
).draw()

scatter plot by manufacturer shape

scatter plot by manufacturer shape

(
    ggplot(data=mpg)
    + geom_point(mapping=aes(x="displ", y="hwy"))
    + facet_wrap("class", nrow=2)
).draw()

scatter plot facets by class

scatter plot facets by class

(
    ggplot(data=mpg, mapping=aes(x="displ", y="hwy"))
    + geom_point()
    + geom_smooth()
).draw()

relationship between engine size and fuel efficiency

relationship between engine size and fuel efficiency

Conclusions

Plotnine provides a powerful and flexible framework for creating high-quality statistical graphics in Python. Its adherence to the Grammar of Graphics makes it intuitive to learn and use, while its extensive customization options allow for the creation of unique and informative visualizations.

Whether you’re a data scientist, researcher, or anyone who needs to effectively communicate data visually, Plotnine is a valuable tool to have in your arsenal.

The benefits of using plotnine include: a) clarity, b) flexibility, c) reproducibility and d) integration.

References

Contact

Portfolio | Linkedin | Twitter


메타데이터
post_id
8f2a300769df
slug
ggplot2-for-python-plotnine-8f2a300769df
url
https://medium.com/h7w/ggplot2-for-python-plotnine-8f2a300769df
canonical_url
https://medium.com/h7w/ggplot2-for-python-plotnine-8f2a300769df
author_url
https://medium.com/@jesuslm
status
ok
fetched_at
2026-07-23 14:22:47