如何用seaborn绘制阴影误差带? [英] How to plot shaded error bands with seaborn?

查看:250
本文介绍了如何用seaborn绘制阴影误差带?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望创建一个如下所示的图,在其中显示一些值和标准偏差.

我有两组值,包含通过两种不同方法获得的均值和标准差.我想用

另一个例子:

将 matplotlib.pyplot 导入为 plt将 numpy 导入为 np将 seaborn 作为 sns 导入sns.set()N = 100x = np.arange(N)mean_1 = 25 + np.random.normal(0.1, 1, N).cumsum()std_1 = 3 + np.random.normal(0, .08, N).cumsum()mean_2 = 15 + np.random.normal(0.2, 1, N).cumsum()std_2 = 4 + np.random.normal(0, .1, N).cumsum()plt.plot(x, mean_1, 'b-', label='mean_1')plt.fill_between(x, mean_1 - std_1, mean_1 + std_1, color='b', alpha=0.2)plt.plot(x, mean_2, 'r--', label='mean_2')plt.fill_between(x, mean_2 - std_2, mean_2 + std_2, color='r', alpha=0.2)plt.legend(title='title')plt.show()

I wish to create a plot like the following, where I show some values alongside standard deviations.

I have two sets of values, containing the mean and standard deviation obtained by two different methods. I thought of doing this with seaborn, but I don't know exactly how to do it since the official example uses pandas DataFrame objects, which I'm not familiar with.

As an example, consider the following starting code:

import seaborn as sns

mean_1 = [10, 20, 30, 25, 32, 43]
std_1 = [2.2, 2.3, 1.2, 2.2, 1.8, 3.5]

mean_2 = [12, 22, 30, 13, 33, 39]
std_2 = [2.4, 1.3, 2.2, 1.2, 1.9, 3.5]

Thank you,

G.

解决方案

Here is a minimal example to create such a plot with the given data. Thanks to vectorization and broadcasting, working with numpy simplifies the code.

import matplotlib.pyplot as plt
import numpy as np

mean_1 = np.array([10, 20, 30, 25, 32, 43])
std_1 = np.array([2.2, 2.3, 1.2, 2.2, 1.8, 3.5])

mean_2 = np.array([12, 22, 30, 13, 33, 39])
std_2 = np.array([2.4, 1.3, 2.2, 1.2, 1.9, 3.5])

x = np.arange(len(mean_1))
plt.plot(x, mean_1, 'b-', label='mean_1')
plt.fill_between(x, mean_1 - std_1, mean_1 + std_1, color='b', alpha=0.2)
plt.plot(x, mean_2, 'r-', label='mean_2')
plt.fill_between(x, mean_2 - std_2, mean_2 + std_2, color='r', alpha=0.2)
plt.legend()
plt.show()

Another example:

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

sns.set()
N = 100
x = np.arange(N)
mean_1 = 25 + np.random.normal(0.1, 1, N).cumsum()
std_1 = 3 + np.random.normal(0, .08, N).cumsum()

mean_2 = 15 + np.random.normal(0.2, 1, N).cumsum()
std_2 = 4 + np.random.normal(0, .1, N).cumsum()

plt.plot(x, mean_1, 'b-', label='mean_1')
plt.fill_between(x, mean_1 - std_1, mean_1 + std_1, color='b', alpha=0.2)
plt.plot(x, mean_2, 'r--', label='mean_2')
plt.fill_between(x, mean_2 - std_2, mean_2 + std_2, color='r', alpha=0.2)

plt.legend(title='title')
plt.show()

这篇关于如何用seaborn绘制阴影误差带?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆