猿问

如何用 seaborn 绘制阴影误差带?

我希望创建一个如下图,在其中显示一些值以及标准偏差。

我有两组值,包含通过两种不同方法获得的平均值和标准差。我想用seaborn做这个,但我不知道具体怎么做,因为官方示例使用了我不熟悉的 pandas DataFrame 对象。

例如,考虑以下起始代码:

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]

谢谢,


G。


德玛西亚99
浏览 353回答 1
1回答

炎炎设计

这是使用给定数据创建此类图的最小示例。由于矢量化和广播,使用 numpy 简化了代码。import matplotlib.pyplot as pltimport numpy as npmean_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()另一个例子:import matplotlib.pyplot as pltimport numpy as npimport seaborn as snssns.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()
随时随地看视频慕课网APP

相关分类

Python
我要回答