Custom markers in a scatter plot with plotnine

logo of a chart:ScatterPlot

Plotnine is a Python library allowing to create beautiful ggplot-like plots using the grammar of graphics syntax.

In this step-by-step post, we will see how to customize markers in a scatter plot using plotnine. For an introduction to scatter plot in plotnine, check this post.

Libraries

For creating this chart, we will need to load the following libraries:

import pandas as pd
from plotnine import *

Dataset

Since scatter plot is a type of chart that displays values for two numerical variables for a set of data, we will load the iris dataset:

url = 'https://raw.githubusercontent.com/holtzy/The-Python-Graph-Gallery/master/static/data/iris.csv'
df = pd.read_csv(url)

Change size of points

In order to change the size of the points in a scatter plot, you just have to add the size argument to the geom_point() function:

(
ggplot(df, aes(x='sepal_length', y='sepal_width')) +
    geom_point(size=4)
)

Change dots shape

In order to change the shape of the points in a scatter plot, you just have to add the shape argument to the geom_point() function.

There are lots of available options!

(
ggplot(df, aes(x='sepal_length', y='sepal_width')) +
    geom_point(shape='d', size=4)
)

Transparency

In order to change the transparency of the points in a scatter plot, you just have to add the alpha argument to the geom_point() function:

(
ggplot(df, aes(x='sepal_length', y='sepal_width')) +
    geom_point(alpha=0.3, size=4)
)

Edge color

In order to change the edge color of the points in a scatter plot, you just have to use the color and fill arguments in the geom_point() function:

(
ggplot(df, aes(x='sepal_length', y='sepal_width')) +
    geom_point(color='black', fill='lightblue', size=4)
)

Going further

This article explains how to custom markers in a scatter plot with plotnine.

If you want to go further, you can also learn how to colors according to a 3rd variable or how to custom theme.

Contact & Edit


👋 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! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!