Obtaining span of plotted points from seaborn swarmplot

Obtaining span of plotted points from seaborn swarmplot,第1张

Obtaining span of plotted points from seaborn swarmplot

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 pdimport numpy as npimport matplotlib.pyplot as pltimport seaborn as sbfrom matplotlib.patches import Rectangle# Generate dummy data.a = np.random.random(75)b = np.random.random(75) - 0.6c = np.random.random(75) + 0.75# Collate into a Dataframedf = 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:        passgetdatalim(ax.collections[0]) # "Parent"getdatalim(ax.collections[1]) # "Offspring"plt.show()

which prints:

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

And here’s the figure:



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4917751.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存