首页 > 解决方案 > 在 for 循环中对 sns.boxplot 和 sns.scatterplot 使用 plt.subplot

问题描述

我一直在尝试使用plt.subplotonsns.boxplotsns.stripplotfor 循环。然而,只显示了一个子图。你知道我该如何解决这个问题吗?这是代码:

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()

这是它的样子:

在此处输入图像描述

我在这里先向您的帮助表示感谢!

标签: pythonmatplotlibseaborn

解决方案


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

说数据是这样的:

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'])

然后这将起作用:

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在调用所有函数之后:

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

在此处输入图像描述


推荐阅读