简体   繁体   English

在 seaborn regplot 中调整误差线的方法

[英]Method to adjust errorbars in seaborn regplot

Background背景
I am plotting my data using sns.regplot (seaborn 0.11.0, Python 3.8.5).我正在使用sns.regplot (seaborn 0.11.0, Python 3.8.5) 绘制我的数据。 I use the argument 'x_estimator' to plot the mean of each category shown on the x-axis, and for each point on the x-axis I have an errorbar which is bootstrapped using the sns.regplot arguments 'ci' and 'boot'.我对 plot 使用参数“x_estimator”来表示 x 轴上显示的每个类别的平均值,对于 x 轴上的每个点,我都有一个错误栏,它使用sns.regplot arguments 'ci' 和 'boot' 引导.

Since this plot needs to have a specific dots per inch (DPI) of 800, I needed to readjust the scaling of the original plot to make sure the desired DPI was obtained.由于此 plot 需要具有 800 的特定每英寸点数 (DPI),因此我需要重新调整原始 plot 的缩放比例以确保获得所需的 DPI。

Problem问题
Due to the rescaling, my errorbars appear to be rather 'wide'.由于重新缩放,我的错误栏似乎相当“宽”。 I would like to make them less wide, and if it is possible, I would also like to add caps on the errorbars.我想让它们不那么宽,如果可能的话,我还想在错误栏上添加大写。 I have included my code below using a randomly generated dataset.我使用随机生成的数据集在下面包含了我的代码。 Running this code, one can see that the plot that I obtain has the correct DPI, but the errorbars are too wide.运行这段代码,可以看到我得到的 plot的 DPI 是正确的,但是误差线太宽了。

Edit for clarification编辑澄清

I am fine with the confidence intervals (CI) in itself.我对置信区间(CI)本身很好。 My only worry is that the CIs are a bit too wide.我唯一担心的是 CI 有点太宽了。 This is probably some formatting issue.这可能是一些格式问题。 I already checked line_kws and scatter_kws but I can't find any formatting options for the CIs.我已经检查了line_kwsscatter_kws但我找不到 CI 的任何格式选项。 My desired output looks like this : the same bars, but not as 'heavy' as the original ones.我想要的 output 看起来像这样:相同的条,但不像原来的那样“重”。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#%%

import matplotlib.pyplot as plt
import numpy             as np
import pandas            as pd
import seaborn           as sns

from matplotlib import rcParams

#%%

# seaborn params
sns.set_style("ticks")
sns.set_context("paper")

# plotting params
rcParams['font.family']     = 'Times New Roman'
rcParams['axes.titlesize']  = 6
rcParams['axes.labelsize']  = 5
rcParams['xtick.labelsize'] = 5
rcParams['ytick.labelsize'] = 5

#%%

# some toy data into to pandas dataframe
df = pd.DataFrame({'Y': np.random.normal(0, 1, (800,)), 
                   'X': np.repeat(range(1, 9), 100), 
                   'Condition': np.tile(["A", "B"], 400)}, 
                  index=range(800))

#%%
    
# make a subplot with 1 row and 2 columns
fig, ax_list = plt.subplots(1, 2,
                            sharex  = True, 
                            sharey  = True,
                            squeeze = True)

# A condition
g = sns.regplot(x           = "X", 
                y           = "Y", 
                data        = df.loc[df["Condition"] == "A"], 
                x_estimator = np.mean, 
                x_ci        = "ci", 
                ci          = 95,
                n_boot      = 5000,
                scatter_kws = {"s":15}, 
                line_kws    = {'lw': .75},
                color       = "darkgrey",
                ax          = ax_list[0])

# B condition
g = sns.regplot(x           = "X", 
                y           = "Y", 
                data        = df.loc[df["Condition"] == "B"], 
                x_estimator = np.mean, 
                x_ci        = "ci", 
                ci          = 95,
                n_boot      = 5000,
                scatter_kws = {"s":15}, 
                line_kws    = {'lw': .75},
                color       = "black",
                ax          = ax_list[1])

# figure parameters (left figure)
ax_list[0].set_title("A condition")   
ax_list[0].set_xticks(np.arange(1, 9))
ax_list[0].set_xlim(0.5, 8.5)
ax_list[0].set_xlabel("X")
ax_list[0].set_ylabel("Y")

# figure parameters (right figure)
ax_list[1].set_title("B condition")   
ax_list[1].set_xlabel("X")
ax_list[1].set_ylabel("Y")

# general title
fig.suptitle("Y ~ X", fontsize = 8) 

#%%

# set the size of the image
fig.set_size_inches(3, 2)

# play around until the figure is satisfactory (difficult due to high DPI)
plt.subplots_adjust(top=0.85, bottom=0.15, left=0.185, right=0.95, hspace=0.075,
                    wspace=0.2)

# save as tiff with defined DPI
plt.savefig(fname = "test.tiff", dpi = 800)

plt.close("all")


Try setting ci parameters in sns.regplot to a lower value尝试将sns.regplot中的ci参数设置为较低的值

I just ran into this problem myself and found a hacky solution.我自己遇到了这个问题,并找到了一个 hacky 解决方案。 It looks like keyword arguments for the confidence intervals (CI) are not yet exposed to the user (see here ).看起来像关键字 arguments 的置信区间 (CI) 尚未向用户公开(请参见此处)。 But we can see that it sets the CI line width to 1.75 * linewidth from mpl.rcParams .但我们可以看到,它将 CI 线宽从mpl.rcParams设置为1.75 * linewidth So I think you can get what you want by hacking a matplotlib rcParams context manager.所以我认为你可以通过破解 matplotlib rcParams 上下文管理器来得到你想要的。

import matplotlib as mpl
import numpy as np
import seaborn as sns

# Insert other code from your question here
# to get your dataframe
df = ...

# Play around with this number until you get the desired line width
line_width_reduction = 0.5

linewidth = mpl.rcParams["lines.linewidth"]
with mpl.rc_context({"lines.linewidth": line_width_reduction * linewidth}):
    g = sns.regplot(
        x="X", 
        y="Y", 
        data=df.loc[df["Condition"] == "A"], 
        x_estimator=np.mean, 
        x_ci="ci", 
        ci=95,
        n_boot=5000,
        scatter_kws={"s":15}, 
        line_kws={'lw': .75},
        color="darkgrey",
        ax=ax_list[0]
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM