Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining span of plotted points from seaborn swarmplot

Tags:

I have the following data:

import pandas as pd
import numpy as np

# Generate dummy data.
a = np.random.random(75)
b = np.random.random(75) - 0.6
c = np.random.random(75) + 0.75 

# Collate into a DataFrame
df = pd.DataFrame({'a': a, 'b': b, 'c': c}) 
df.columns = [list(['WT', 'MUT', 'WTxMUT']), list(['Parent', 'Parent', 'Offspring'])]
df.columns.names = ['Genotype', 'Status']
df_melt = pd.melt(df) 

and I plot it in seaborn using this code:

import seaborn as sb
sb.swarmplot(data = df_melt, x = "Status", y = "value", hue = "Genotype")

Link to swarmplot output

How do I get the x-span of each group? What is the range of the horizontal span of the swarmplot for the Parent group, for instance?

like image 507
Joses Ho Avatar asked Apr 14 '16 06:04

Joses Ho


People also ask

How do I read a Swarmplot file?

A swarm plot is another way of plotting the distribution of an attribute or the joint distribution of a couple of attributes. The 'x-axis' depicts the carat and this plot helps us to understand at what carat are there more diamonds or how the diamond count varies with carat size.

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.

How do you show multiple plots in Seaborn?

In Seaborn, we will plot multiple graphs in a single window in two ways. First with the help of Facetgrid() function and other by implicit with the help of matplotlib. data: Tidy dataframe where each column is a variable and each row is an observation.

What is the command to plot a Swarmplot in Seaborn?

Syntax: sns. swarmplot(x, y, hue, data);


1 Answers

You can get the information from the collections which are created by swarmplot.

swarmplot actually returns the matplotlib Axes instance, and from there we can find the PathCollections that it creates. To get the positions, we can use .get_offsets().

Here is your example, modified to find and print the swarm limits, and then use them to plot a box around the swarms.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sb
from matplotlib.patches import Rectangle

# Generate dummy data.
a = np.random.random(75)
b = np.random.random(75) - 0.6
c = np.random.random(75) + 0.75 

# Collate into a DataFrame
df = pd.DataFrame({'a': a, 'b': b, 'c': c}) 
df.columns = [list(['WT', 'MUT', 'WTxMUT']), list(['Parent', 'Parent', 'Offspring'])]
df.columns.names = ['Genotype', 'Status']
df_melt = pd.melt(df) 

ax = sb.swarmplot(data = df_melt, x = "Status", y = "value", hue = "Genotype")

def getdatalim(coll):
    x,y = np.array(coll.get_offsets()).T
    try:
        print 'xmin={}, xmax={}, ymin={}, ymax={}'.format(
                x.min(), x.max(), y.min(), y.max())
        rect = Rectangle((x.min(),y.min()),x.ptp(),y.ptp(),edgecolor='k',facecolor='None',lw=3)
        ax.add_patch(rect)
    except ValueError:
        pass

getdatalim(ax.collections[0]) # "Parent"
getdatalim(ax.collections[1]) # "Offspring"

plt.show()

which prints:

xmin=-0.107313729132, xmax=0.10661092707, ymin=-0.598534246847, ymax=0.980441247759
xmin=0.942829146473, xmax=1.06105941656, ymin=0.761277608688, ymax=1.74729717464

And here's the figure:

enter image description here

like image 135
tmdavison Avatar answered Sep 28 '22 03:09

tmdavison