Libraries

First, we need to load a few libraries:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

Dataset

Since scatter plot are made for visualizing relationships between two numerical variables, we need a dataset that contains at least two numerical columns.

Here, we will use the iris dataset that we load directly from the gallery:

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

Control Marker Shape

In order to change the shape of the marker, you need to provide:

  • marker: the shape of the marker

There are actually lots of different shapes available. Here are a few examples:

  • 'o': circle
  • 's': square
  • '+': plus
  • 'x': cross
  • 'D': diamond
  • 'h': hexagon
  • 'p': pentagon
  • 'v': triangle_down
  • '^': triangle_up
  • '<': triangle_left
  • '>': triangle_right
  • all numbers from 1 to 4
  • and many more...

You can find them by running the following code:

from matplotlib import markers
print(markers.MarkerStyle.markers.keys())
fig, ax = plt.subplots(figsize=(8, 6))
sns.regplot(
    x=df["sepal_length"],
    y=df["sepal_width"],
    marker="+",
    fit_reg=False,
    ax=ax
)

plt.show()

Changing Color, Transparency and Size of Markers

You can also change the other features of markers in a plot. The following arguments must be provided:

  • color : color of the markers
  • alpha : opacity of the markers
  • s : size of the markers
fig, ax = plt.subplots(figsize=(8, 6))
sns.regplot(
    x=df["sepal_length"],
    y=df["sepal_width"],
    fit_reg=False,
    scatter_kws={
        "color":"darkred",
        "alpha":0.3,
        "s":200
    },
    ax=ax
)
plt.show()

Going further

This post explains how to customize the appearance of the markers in a scatter plot with seaborn.

You might be interested in how to create a bubble plot, a kind of scatter plot where the size of the marker is proportional to a third variable or how to colors dots according to a variable.

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 🙏!