首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在sns.boxplot上使用plt.subplot并在for循环中使用sns.scatterplot

在sns.boxplot上使用plt.subplot并在for循环中使用sns.scatterplot
EN

Stack Overflow用户
提问于 2021-07-30 11:29:07
回答 1查看 185关注 0票数 0

我一直在尝试在sns.boxplot上使用plt.subplot,并在for循环中使用sns.stripplot。然而,只显示了一个子图。你知道我该怎么解决这个问题吗?代码如下:

代码语言:javascript
运行
复制
for Rab, pRab in zipped: 
    sns.set(font_scale = 2)
    with sns.axes_style(style='ticks'):
            fig, axes = plt.subplots(1,2, figsize =(18,6))
            plt.tight_layout(pad=3.0)            
            sns.stripplot(
                ax = axes[0],
                data = unphospho_ratio,
                y = Rab,
                x = 'disease state',
                color = 'black',
                s = 8)
  
            sns.boxplot(
                ax = axes[0],
                data = unphospho_ratio,
                y = Rab,
                x = 'disease state')
            sns.despine(offset = 10, trim = True)
            plt.show()
            
            sns.stripplot(
                ax = axes[1],
                data = phospho_ratio,
                y = pRab,
                x = 'disease state',
                color = 'black',
                s = 8)

            sns.boxplot(
                ax = axes[1],
                data = phospho_ratio,
                y = pRab,
                x = 'disease state')
            sns.despine(offset = 10, trim = True)
            plt.show()

这是它看起来是什么样的图像:

提前感谢您的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-30 20:39:09

您一直在相同的轴上绘图,即对于第一个箱线图和条形图,您使用的是axes[0]

假设数据是这样的:

代码语言:javascript
运行
复制
unphospho_ratio = pd.DataFrame({'disease state':np.random.choice(['PD','Ctrl'],20),
                               'y1a':np.random.uniform(0,1,20),
                               'y2a':np.random.uniform(1,2,20)})
phospho_ratio = pd.DataFrame({'disease state':np.random.choice(['PD','Ctrl'],20),
                               'y1b':np.random.uniform(0,1,20),
                              'y2b':np.random.uniform(1,2,20)})
zipped = zip(['y1a','y2a'],['y1b','y2b'])

然后这将会起作用:

代码语言:javascript
运行
复制
for Rab, pRab in zipped: 
    fig, axes = plt.subplots(1,2, figsize =(18,6))
    plt.tight_layout(pad=3.0)            
    sns.stripplot(
        ax = axes[0],
        data = unphospho_ratio,
        y = Rab,
        x = 'disease state',
        color = 'black',
        s = 8)
    sns.boxplot(
        ax = axes[1],
        data = unphospho_ratio,
        y = Rab,
        x = 'disease state')
    sns.despine(offset = 10, trim = True)
    plt.show()
            
    sns.stripplot(
        ax = axes[0],
        data = phospho_ratio,
        y = pRab,
        x = 'disease state',
        color = 'black',
        s = 8)

    sns.boxplot(
        ax = axes[1],
        data = phospho_ratio,
        y = pRab,
        x = 'disease state')
    sns.despine(offset = 10, trim = True)
    plt.show()

如果你想要叠加点和箱形图(从你的问题中看不清楚),你不想看绘图设备plt,它应该在调用所有函数后是fig

代码语言:javascript
运行
复制
for Rab, pRab in zipped: 
    fig, axes = plt.subplots(1,2, figsize =(18,6))
    plt.tight_layout(pad=3.0)            
    sns.stripplot(
        ax = axes[0],
        data = unphospho_ratio,
        y = Rab,
        x = 'disease state',
        color = 'black',
        s = 8)
    sns.boxplot(
        ax = axes[0],
        data = unphospho_ratio,
        y = Rab,
        x = 'disease state')
    sns.despine(offset = 10, trim = True)
            
    sns.stripplot(
        ax = axes[1],
        data = phospho_ratio,
        y = pRab,
        x = 'disease state',
        color = 'black',
        s = 8)

    sns.boxplot(
        ax = axes[1],
        data = phospho_ratio,
        y = pRab,
        x = 'disease state')
    sns.despine(offset = 10, trim = True)
    
fig

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68590233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档