GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • All
  • Best
  • Libs
  • Learn

This view

  • Original page
  • New address

GeezerVison is showing python-graph-gallery.com — Plotnine

Plotnine: ggplot in python

logo of a chart:plotnine

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.

Plotnine section

⏱ Quick start

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

pip install plotnine

The Grammar of Graphics is a way of thinking about how to build graphs. It is based on the idea that you can build a graph by adding layers to it. In this example, we add a layer of points to a graph.

Once installed, you can either import all the functions from the library with from plotnine import * or import only the functions you need with from plotnine import ggplot, geom_point.

Most basic plotnine chart

Most basic plotnine chart

# 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 plot with plotnine

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.

Most basic scatterplot with plotnine

Most basic scatterplot with plotnine

Change color markers

Change color markers

Color by group

Color by group

Change marker size

Change marker size

Changer marker shape

Changer marker shape

Changer marker transparency

Changer marker transparency

Custom edge colors

Custom edge colors

Bar plot with plotnine

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.

Most simple barplot

Most simple barplot

Change color and edge color

Change color and edge color

Custom width of the bars

Custom width of the bars

Define one specific color per bar

Define one specific color per bar

Color bars according to a variable

Color bars according to a variable

Flip the x and y axis

Flip the x and y axis

Histogram with plotnine

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.

Most basic histogram with plotnine

Most basic histogram with plotnine

Control bar color

Control bar color

Custom style with theme, colors and opacity

Custom style with theme, colors and opacity

Split histogram by group

Split histogram by group

Mirror histogram to compare 2 variables

Mirror histogram to compare 2 variables

Change theme with plotnine

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

White theme with plotnine

White theme with plotnine

Dark theme with plotnine

Dark theme with plotnine

Theme withouth grid lines

Theme withouth grid lines

Related chart types

logo of a chart:Colours

Colors

logo of a chart:Interactive

Interactivity

logo of a chart:anim

Animation

logo of a chart:Cheat

Cheat sheets

logo of a chart:Bad

Caveats

logo of a chart:3d

3D

logo of a chart:Stats

Statistics

300 projects

💡 Out of dataviz inspiration?

I have spent years collecting the most beautiful data visualizations out there. Get the list of my top 300 dataviz projects, hand-picked, for free.

I respect your privacy. Unsubscribe anytime.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.