# Draw seaborn lineplot with error band based on the standard deviation
fig, ax = plt.subplots(figsize=(9,5))
sns.lineplot(data=df, x="timepoint", y="signal", ci='sd')
sns.despine()
plt.show()
# 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()
型
的 如果您更喜欢使用误差线,则海运线图如下所示:
# 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)
2条答案
按热度按时间cgh8pdjw1#
从
seaborn v0.12
,ci
参数已更改为errorbar
。在下面的示例中,使用errorbar='sd'
而不是ci='sd'
。还有errorbar=('sd', n)
,其中n
是标准差的数量。如果您需要
seaborn.lineplot
提供的误差带/条以外的误差带/条,你必须自己绘制它们。这里有几个例子,说明如何在matplotlib中绘制误差带和误差条,并获得与seaborn中类似的图。它们是用 fmri 构建的。示例数据集作为pandas框架导入,并基于lineplot function上的seaborn文档中显示的示例之一。字符串
这个数据集包含一个名为 timepoint 的时间变量,在19个时间点中的每个时间点上有56个 * 信号 * 的测量值。我使用默认的估计值,即平均值。为了简单起见,而不是使用平均值的标准误差的置信区间作为不确定性的度量(又名误差),我使用每个时间点测量的标准差。这是通过传递
ci='sd'
在lineplot
中设置的,误差扩展到平均值两侧的一个标准差(即对称)。以下是带有误差带的海运线图(默认情况下):型
x1c 0d1x的数据
现在假设我更喜欢在平均值两侧的每个时间点上设置一个误差带,该误差带跨越测量值的标准差的一半。由于在调用
lineplot
函数时无法设置此首选项,因此据我所知,最简单的解决方案是使用matplotlib从头开始创建图。型
的
如果您更喜欢使用误差线,则海运线图如下所示:
型
的
以下是如何使用自定义误差条使用matplotlib获得相同类型的图:
型
的
Matplotlib文档:fill_between,specify error bars,subsample error bars
Pandas文档:错误条
4ioopgfo2#
我可以通过在
lineplot
本身返回的轴上调用fill_between
来实现这一点:字符串
生成的图像:
的数据
作为参考,
dataset
是一个pandas.DataFrame
,看起来像:型