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

  • PYTHON CHARTS
  • matplotlib
  • seaborn
  • plotly
  • pandas
  • colors
  • COLORS
  • COLOR PALETTES
  • R CODER
  • R CHARTS
  • Contact
  • Español

This view

  • Original page
  • New address

GeezerVison is showing python-charts.com — Scatter plot by group in seaborn

Search for a graph

HOME CORRELATION SCATTER PLOT GROUP SEABORN

Scatter plot by group in seaborn

Sample data

The data of the following block of code will be used as the sample data of this tutorial.

import numpy as np
import pandas as pd
from random import choices

# Seed
rng = np.random.RandomState(0)

# Data simulation
x = rng.uniform(0, 1, 500)
y = 5 * x + rng.normal(0, 2, size = 500)
group = np.where(x < 0.4, "A", np.where(x > 0.8, "C", "B"))
group2 = choices(["G1", "G2"], k = 500)
x = x + rng.uniform(-0.2, 0.2, 500)

# Data set
df = {'x': x, 'y': y, 'group': group, 'group2': group2}

Scatter plot by group with the hue and style arguments

Basic scatter plot by group

Using the scatterplot function from seaborn it is very easy to create a scatter plot by group. You will need to pass your grouping variable to the hue argument of the function.

import seaborn as sns

sns.scatterplot(x = x, y = y, hue = group)

Scatter plot by group in seaborn

Pass the variables as keys of the data frame

Note that you can also specify a data frame and pass the variables as keys. In this scenario, the legend will also show the name of the variable as title.

import seaborn as sns

sns.scatterplot(x = "x", y = "y", hue = "group",
                data = df)

Scatter plot based on a categorical variable in seaborn

Order of the levels

The order of the categorical variable is based on the order of appearance in the data. If you want to reorder the hue order you can create the desired order and pass it to the hue_order argument, as shown below.

import seaborn as sns

hue_order = ["A", "B", "C"]

sns.scatterplot(x = x, y = y,
                hue = group, hue_order = hue_order)

Order of the groups or levels of the scatter plot in seaborn

Markers by group

If you pass the categorical variable to style the markers will have a different shape based on the groups. Note that if you keep a single color across groups the markers will be difficult to recognize, so you should also set the hue argument.

import seaborn as sns

sns.scatterplot(x = x, y = y,
                style = group, hue = group)

Scatter plot with markers by group in seaborn

Two groups based on the color and the style of the markers

In case your data contains another categorical variable you can pass a variable to style and the other to hue. Nonetheless, you should be aware that the variable passed to hue will be easier to read.

import seaborn as sns

sns.scatterplot(x = x, y = y,
                style = group, hue = group2)

Scatter plot by two groups in seaborn

Color and markers customization

Custom color for each group in a scatter plot with seaborn in Python

Custom color palette

In order to add a custom palette you can create a dictionary with the desired colors and pass it to the palette argument of the function.

import seaborn as sns

colors = {'A': 'blue', 'B': 'red', 'C': 'green'}

sns.scatterplot(x = x, y = y,
                hue = group, palette = colors)

Change the shape of the markers based on groups in seaborn

Custom markers style

Similarly, you can change the markers shape. Possible values are ',', '.', 'o', 'd', 'h', 's', 'p', 'v', '^', '<', '>', '8', '*', 'H', 'D', 'P' and 'X' for the scatterplot function.

import seaborn as sns

styles = {'A': 'p', 'B': 's', 'C': 'v'}

sns.scatterplot(x = x, y = y,
                style = group, hue = group,
                markers = styles, palette = "Set1")
R CODER website
Data visualization

Data visualization

A practical introduction

Buy on Amazon
Better Data Visualizations

Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon

See also

Heatmap clustering in seaborn with clustermap

Heatmap clustering in seaborn with clustermap

Heat map in matplotlib

Heat map in matplotlib

Heat map in seaborn with the heatmap function

Heat map in seaborn with the heatmap function

Scatter plot in plotly

Scatter plot in plotly

Scatter plot in matplotlib

Scatter plot in matplotlib

Scatter plot with marginal histograms in seaborn

Scatter plot with marginal histograms in seaborn

Hexbin chart in matplotlib

Hexbin chart in matplotlib

Heatmaps in plotly with imshow

Heatmaps in plotly with imshow

2D histograms in plotly

2D histograms in plotly

Histograms in plotly

Histograms in plotly

You are visitor 01402792 — 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.