【问题标题】:Fill in Area between curves Python [duplicate]填充曲线Python之间的区域[重复]
【发布时间】:2021-05-07 12:14:54
【问题描述】:

如何填充下方曲线“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')

输出:

【问题讨论】:

标签: python python-3.x matplotlib plot


【解决方案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()

这段代码将产生以下结果:

【讨论】:

  • 我收到此错误,因为我正在使用敏感度图:TypeError: ufunc 'isfinite' not supported for the input types, and the input can not be safe to becovered to any supported types based on the cast rule ''安全''
  • 我不确定是什么导致了问题,但错误基本上表明函数isfinite 没有为numpy 数组中的数据的dtype 定义。也许你可以看看this answerthis one。您可能需要将要绘制的数据转换为float(使用data=np.array(data,dtype=float)