填充曲线之间的区域 Python

时间:2021-02-03 09:24:42

标签: python python-3.x matplotlib plot

如何填充曲线“a”(向上倾斜的线)下方上方“b”(水平)的空间?谢谢。

#Plot data
sns.set(font_scale = 1.5, style = 'white', rc=None)

fig, ax = plt.subplots(figsize = (15,10))

a = sensitivity.plot(y = 0.2, ax = ax, linestyle = '--', color = 'gray')
b = sensitivity.plot(y = 0.3, ax = ax, linestyle = '-.', color = 'gray')
c = sensitivity.plot(y = 0.4, ax = ax, linestyle = ':', color = 'gray')
d = ax.hlines(y=7.5, xmin=100, xmax=900, colors='black', linestyles='-', lw=2, label='Single Short Line')

输出:

enter image description here

1 个答案:

答案 0 :(得分:1)

正如 DavidG 所建议的,您确实可以使用 ax.fill_between。有关示例,请参阅此 page。除了那个页面,我将在下面展示一个非常简单的例子:

import numpy as np
import matplotlib.pyplot as plt

# Generate some data
x  = np.linspace(0, 3, 25)
y1 = x**2 + 1   # Main data
y2 = 0.85 * y1  # Lower boundary
y3 = 1.15 * y1  # Upper boundary

# Create a figure and axes
fig, ax = plt.subplots()

# This creates the blue shaded area
ax.fill_between(x, y2, y3, alpha=0.3) 

# Here, we plot the solid line in the 'center'
ax.plot(x, y1, c='k') 

# Here, we plot the boundaries of the blue shaded area
ax.plot(x, y2, c='k', ls='--', alpha=0.3)  # Lower boundary
ax.plot(x, y3, c='k', ls='--', alpha=0.3)  # Upper boundary

plt.show()

这段代码会产生如下结果: Sample plot using ax.fill_between