Matplotlib 如何在seaborn线型图中使用自定义错误栏

Matplotlib 如何在seaborn线型图中使用自定义错误栏,matplotlib,data-visualization,seaborn,Matplotlib,Data Visualization,Seaborn,我正在使用seaborn.lineplot生成一些时间序列图。我在两个列表中预先计算了一种特定类型的错误条,例如,upper=[1,2,3,4,5]lower=[0,1,2,3,4]。有没有一种方法可以在这里自定义错误栏,而不是在lineplot中使用CI或Std错误栏?是的,seaborn只是在幕后使用matplotlib,这样你基本上可以做任何你想做的事,包括使用matplotlib的plt.errorbar添加您自己的自定义errorbar,或者如果您想 话虽如此,你或许可以先尝试调整se

我正在使用
seaborn.lineplot
生成一些时间序列图。我在两个列表中预先计算了一种特定类型的错误条,例如,
upper=[1,2,3,4,5]lower=[0,1,2,3,4]
。有没有一种方法可以在这里自定义错误栏,而不是在
lineplot
中使用CI或Std错误栏?

是的,seaborn只是在幕后使用matplotlib,这样你基本上可以做任何你想做的事,包括使用matplotlib的
plt.errorbar
添加您自己的自定义errorbar,或者如果您想

话虽如此,你或许可以先尝试调整seaborn的选项。以我的经验,它提供了一个非常广泛的定制面板,并具有制作非常整洁的视觉效果的优势

如果您对seaborn不满意,但不想深入matplotlib API,另一种选择是使用seaborn创建错误栏,然后在方便的时候调整它们(如我所说,它们只是matplotlib对象)


如果您提供一个更具体的示例,我可能能够提供更多帮助。如果您想要错误带/条,而不是
seaborn.lineplot提供的错误带/条,您必须自己绘制它们。下面是几个示例,说明如何在matplotlib中绘制错误带和错误条,并获得与seaborn中类似的绘图。它们是使用作为熊猫数据框架导入的功能磁共振样本数据集构建的,并且基于屏幕上seaborn文档中显示的一个示例

该数据集包含一个名为timepoint的时间变量,在19个时间点中的每个时间点上对信号进行56次测量。我使用默认估计值,即平均值。为了简单起见,我没有使用平均值标准误差的置信区间作为不确定度的度量(也称为误差),而是使用每个时间点测量值的标准偏差。这是通过传递
ci='sd'
lineplot
中设置的,误差扩展到平均值每侧的一个标准偏差(即对称)。以下是seaborn线型图的外观,带有错误标注栏(默认情况下):

现在,让我们假设我更喜欢有一个误差带,它跨越平均值每边每个时间点测量值的半个标准偏差。由于调用
lineplot
函数时无法设置此首选项,据我所知,最简单的解决方案是使用matplotlib从头开始创建绘图

# Matplotlib plot with custom error band

# Define variables to plot
y_mean = df.groupby('timepoint').mean()['signal']
x = y_mean.index

# Compute upper and lower bounds using chosen uncertainty measure: here
# it is a fraction of the standard deviation of measurements at each
# time point based on the unbiased sample variance
y_std = df.groupby('timepoint').std()['signal']
error = 0.5*y_std
lower = y_mean - error
upper = y_mean + error

# Draw plot with error band and extra formatting to match seaborn style
fig, ax = plt.subplots(figsize=(9,5))
ax.plot(x, y_mean, label='signal mean')
ax.plot(x, lower, color='tab:blue', alpha=0.1)
ax.plot(x, upper, color='tab:blue', alpha=0.1)
ax.fill_between(x, lower, upper, alpha=0.2)
ax.set_xlabel('timepoint')
ax.set_ylabel('signal')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.show()

如果希望有错误条,则seaborn线型图如下所示:

# Draw seaborn lineplot with error bars based on the standard deviation
fig, ax = plt.subplots(figsize=(9,5))
sns.lineplot(data=df, x="timepoint", y="signal", ci='sd', err_style='bars')
sns.despine()
plt.show()

以下是如何使用自定义错误栏获得与matplotlib相同类型的打印:

# Matplotlib plot with custom error bars

# If for some reason you only have lists of the lower and upper bounds
# and not a list of the errors for each point, this seaborn function can
# come in handy:
# error = sns.utils.ci_to_errsize((lower, upper), y_mean)

# Draw plot with error bars and extra formatting to match seaborn style
fig, ax = plt.subplots(figsize=(9,5))
ax.errorbar(x, y_mean, error, color='tab:blue', ecolor='tab:blue')
ax.set_xlabel('timepoint')
ax.set_ylabel('signal')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.show()

# Note: in this example, y_mean and error are stored as pandas series
# so the same plot can be obtained using this pandas plotting function:
# y_mean.plot(yerr=error)

Matplotlib文档:


熊猫文档:

FYI:彻底回答问题很费时。如果你的问题得到了解决,那就接受最符合你需要的解决方案来表示感谢。这个✔ 低于▲/▼ 箭头,在答案的左上角。如果出现更好的解决方案,则可以接受新的解决方案。你也可以用以下方式投票决定答案的有用性:▲/▼ 阿罗,如果你有15+的声望。如果解决方案不能回答问题,请留下评论。非常感谢。
# Draw seaborn lineplot with error bars based on the standard deviation
fig, ax = plt.subplots(figsize=(9,5))
sns.lineplot(data=df, x="timepoint", y="signal", ci='sd', err_style='bars')
sns.despine()
plt.show()
# Matplotlib plot with custom error bars

# If for some reason you only have lists of the lower and upper bounds
# and not a list of the errors for each point, this seaborn function can
# come in handy:
# error = sns.utils.ci_to_errsize((lower, upper), y_mean)

# Draw plot with error bars and extra formatting to match seaborn style
fig, ax = plt.subplots(figsize=(9,5))
ax.errorbar(x, y_mean, error, color='tab:blue', ecolor='tab:blue')
ax.set_xlabel('timepoint')
ax.set_ylabel('signal')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.show()

# Note: in this example, y_mean and error are stored as pandas series
# so the same plot can be obtained using this pandas plotting function:
# y_mean.plot(yerr=error)