matplotlib 图例中误差带的线条加上阴影区域

Som*_*ody 5 matplotlib legend uncertainty

我想要一个类似的传奇 带有不确定性带的图

但虚线和黄色区域合并如下:

在此输入图像描述

Pab*_*Mur 4

要执行您想要的操作,您需要legend在单个项目中调用组合您想要的两条线/补丁。

要了解如何在实践中做到这一点,这里有一个简单的工作示例:

# Import libraries
import numpy as np
import matplotlib.pyplot as plt

# Create some fake data
xvalue = np.linspace(1,100,100)
pop_mean = xvalue
walker_pos = pop_mean + 10*np.random.randn(100)

# Do the plot
fig, ax = plt.subplots()

# Save the output of 'plot', as we need it later
lwalker, = ax.plot(xvalue, walker_pos, 'b-')

# Save output of 'fill_between' (note there's no comma here)
lsigma = ax.fill_between(xvalue, pop_mean+10, pop_mean-10, color='yellow', alpha=0.5)

# Save the output of 'plot', as we need it later
lmean, = ax.plot(xvalue, pop_mean, 'k--')

# Create the legend, combining the yellow rectangle for the 
# uncertainty and the 'mean line'  as a single item
ax.legend([lwalker, (lsigma, lmean)], ["Walker position", "Mean + 1sigma range"], loc=2)

fig.savefig("legend_example.png")
plt.show()
Run Code Online (Sandbox Code Playgroud)

此代码产生下图:legend_example.png

您可以查看图例指南以了解正在发生的情况,并根据您的需要调整图例。