Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a swarm plot with matplotlib

I know the question is not very informative.. but as I do not know the name of his type of plot, I can not be more informative..

[EDIT] I changed the title, and now it is more informative...

enter image description here

like image 863
Oren Avatar asked Mar 22 '16 11:03

Oren


People also ask

What is swarm plot in Python?

Practical Data Science using Python Swarm Plot in Seaborn is used to draw a categorical scatterplot with non-overlapping points. The seaborn. swarmplot() is used for this. Let's say the following is our dataset in the form of a CSV file − Cricketers.csv.

What is the difference between Swarmplot and Stripplot?

Strip and swarm plots The main difference is that in a swarm plot, the data points don't overlap and are adjusted along the categorical axis. On the other hand, the issue of point overlapping in a strip plot can be partially fixed by setting the alpha parameter that regulates point transparency.

What is bee swarm plot?

A bee swarm plot is a one-dimensional scatter plot similar to stripchart, but with various methods to separate coincident points such that each point is visible. Also, beeswarm introduces additional features unavailable in stripchart, such as the ability to control. the color and plotting character of each point.


1 Answers

If (for whatever reason) you don't want to use seaborn, you can have a go at making them yourself (see e.g. this explanation: https://www.flerlagetwins.com/2020/11/beeswarm.html ).

A simple version is:

#!/usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np


def simple_beeswarm(y, nbins=None):
    """
    Returns x coordinates for the points in ``y``, so that plotting ``x`` and
    ``y`` results in a bee swarm plot.
    """
    y = np.asarray(y)
    if nbins is None:
        nbins = len(y) // 6

    # Get upper bounds of bins
    x = np.zeros(len(y))
    ylo = np.min(y)
    yhi = np.max(y)
    dy = (yhi - ylo) / nbins
    ybins = np.linspace(ylo + dy, yhi - dy, nbins - 1)

    # Divide indices into bins
    i = np.arange(len(y))
    ibs = [0] * nbins
    ybs = [0] * nbins
    nmax = 0
    for j, ybin in enumerate(ybins):
        f = y <= ybin
        ibs[j], ybs[j] = i[f], y[f]
        nmax = max(nmax, len(ibs[j]))
        f = ~f
        i, y = i[f], y[f]
    ibs[-1], ybs[-1] = i, y
    nmax = max(nmax, len(ibs[-1]))

    # Assign x indices
    dx = 1 / (nmax // 2)
    for i, y in zip(ibs, ybs):
        if len(i) > 1:
            j = len(i) % 2
            i = i[np.argsort(y)]
            a = i[j::2]
            b = i[j+1::2]
            x[a] = (0.5 + j / 3 + np.arange(len(b))) * dx
            x[b] = (0.5 + j / 3 + np.arange(len(b))) * -dx

    return x


fig = plt.figure(figsize=(2, 4))
fig.subplots_adjust(0.2, 0.1, 0.98, 0.99)
ax = fig.add_subplot(1, 1, 1)
y = np.random.gamma(20, 10, 100)
x = simple_beeswarm(y)
ax.plot(x, y, 'o')
fig.savefig('bee.png')

a beeswarm plot

like image 182
Michael Clerx Avatar answered Sep 19 '22 12:09

Michael Clerx