栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python:一个点中的seapoint点图和箱线图但在x轴上移动

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python:一个点中的seapoint点图和箱线图但在x轴上移动

为了平移绘图上的点,可以使用变换。在这种情况下,a

ScaledTranslation
是有用的。不幸的是,seaborn不允许直接使用变换,也无法访问绘制的对象。因此,需要从轴获取绘制的对象(在本例中为PathCollection)。如果要偏移的图是轴上的第一个图
ax
,我们可以简单地通过获取它
ax.collections[0]
。然后我们可以通过设置转换
.set_transform

fig, ax = plt.subplots()sns.pointplot(... , ax=ax)#produce transform with 5 points offset in x directionoffset = transforms.ScaledTranslation(5/72., 0, ax.figure.dpi_scale_trans)trans = ax.collections[0].get_transform()ax.collections[0].set_transform(trans + offset)

完整的代码:

import pandas as pdimport numpy as npimport seaborn as snsimport matplotlib.pyplot as pltimport matplotlib.transforms as transformstrial_vec    = np.tile(np.arange(16)+1, 10)     stimulus_vec = np.repeat([-2., -1.75, -1., -0.75, -0.5,  0.5,  1.,  1.25,  1.75,  2.5 ], 16)data_vec     = np.random.randint(0, 16, size=160)spi_num      = pd.Dataframe({'trial': trial_vec,        'stimulus': stimulus_vec, 'data': data_vec})fig, ax = plt.subplots()sns.pointplot(x="stimulus", y="data", data=spi_num, linestyles='', scale=1,    color='k', errwidth=1.5, capsize=0.2, markers='x', ax=ax)#produce transform with 5 points offset in x directionoffset = transforms.ScaledTranslation(5/72., 0, ax.figure.dpi_scale_trans)trans = ax.collections[0].get_transform()ax.collections[0].set_transform(trans + offset)sns.swarmplot(x="stimulus", y="data", data=spi_num, edgecolor="black", linewidth=.9, ax=ax)sns.boxplot(x="stimulus", y="data", data=spi_num, saturation=1, ax=ax)sns.pointplot(x="stimulus", y="data", data=spi_num, linestyles='--', scale=0.4,    color='k', errwidth=0, capsize=0, ax=ax)plt.ylabel("number of spikes")plt.title("Median Number of Spikes");plt.show()

为了转移lineplot还有,你需要做的与上面相同,其散射点(

ax.collections[1]
),并在情节的所有行(
ax.lines

sns.pointplot(x="stimulus", y="data", data=spi_num, linestyles='--', scale=0.4,    color='k', errwidth=0, capsize=0, ax=ax, gid="Nm")# shift points of connecting line:trans = ax.collections[1].get_transform()ax.collections[1].set_transform(trans + offset)# shift everything else:for line in ax.lines:    trans = line.get_transform()    line.set_transform(trans + offset)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/661502.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号