Fundamentals 8 min read

Master Plotnine: Bring ggplot2’s Power to Python for Stunning Visualizations

This guide introduces Plotnine, a Python implementation of ggplot2, covering installation, data preparation, quick plotting with qplot, building layered graphics, customizing colors, sizes, gradients, creating bar and line charts, and links to further documentation.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master Plotnine: Bring ggplot2’s Power to Python for Stunning Visualizations

What is Plotnine?

Plotnine is a Python implementation of the grammar of graphics, providing most of ggplot2’s functionality and enabling R‑style visualizations directly in Python.

Installation

pip install plotnine

Preparing Data

Example uses the built‑in mpg dataset from plotnine.data.

from plotnine.data import mpg
mpg.head()

Quick Plot with qplot

Creates a simple scatter plot with qplot(x='displ', y='cty', data=mpg).

from plotnine import qplot
qplot(x='displ', y='cty', data=mpg)

Building Layers with ggplot

Start with a base layer and add geometries.

from plotnine import ggplot, aes, geom_point
ggplot(aes(x='displ', y='cty'), mpg) + geom_point()

Customizing Aesthetics

Color by cylinder, treat cylinder as a factor, adjust point size, and apply a gradient color scale.

# Color by cyl
ggplot(aes(x='displ', y='cty'), mpg) + geom_point(aes(color='cyl'))

# Factor cyl
ggplot(aes(x='displ', y='cty'), mpg) + geom_point(aes(color='factor(cyl)'))

# Size by highway mpg
ggplot(aes(x='displ', y='cty'), mpg) + geom_point(aes(size='hwy'))

# Gradient color
from plotnine import scale_color_gradient
ggplot(aes(x='displ', y='cty'), mpg) + geom_point(aes(color='hwy')) + scale_color_gradient(low='blue', high='red')

Bar Chart Example

Creates a grouped bar chart from a custom DataFrame.

import pandas as pd
df = pd.DataFrame({
    'variable': ['gender','gender','age','age','age','income','income','income','income'],
    'category': ['Female','Male','1-24','25-54','55+','Lo','Lo-Med','Med','High'],
    'value': [60,40,50,30,20,10,25,25,40]
})
df['variable'] = pd.Categorical(df['variable'], categories=['gender','age','income'])
df['category'] = pd.Categorical(df['category'])
from plotnine import ggplot, aes, geom_col, geom_text, position_dodge

dodge_text = position_dodge(width=0.9)
(
    ggplot(df, aes(x='variable', y='value', fill='category')) +
    geom_col(position='dodge', show_legend=False) +
    geom_text(aes(y=-0.5, label='category'), position=dodge_text,
              color='gray', size=8, angle=30, va='top') +
    lims(y=(-5,60))
)

Line Chart Example

Plots a time‑series from the economics_long dataset.

from plotnine.data import economics_long
from plotnine import ggplot, aes, geom_line

ggplot(economics_long, aes(x='date', y='value01', color='variable')) + geom_line()

Further Resources

Plotnine documentation and the original ggplot2 reference are recommended for deeper learning.

https://plotnine.readthedocs.io/en/latest/

https://ggplot2.tidyverse.org/reference/index.html

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

data analysisTutorialData visualizationggplot2plotnine
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.