python - Matplotlib 在多行之间填充

标签 python matplotlib

我想在 matplotlib.pyplot 的 3 行之间填充,但不幸的是 fill_between 让我有机会只在两行之间填充。任何想法如何处理这个?

编辑:

好的,我没有解释我的真正意思,因为我无法以我目前的声誉添加图片,所以可能是这样:

我尝试填充由这些线界定的多边形,但我不知道如何填充,因为 fill_between 让我有机会仅填充其中两条线之间的区域。填充方程下方:

y <= 4- 2x
y <= 3 - 1/2x
y <= 1 - x
y >= 0
x >= 0

x 和 y 大于 0 是显而易见的。我从 (0,0) 开始情节,但我还有 3 行...

y <= 4- 2x
y <= 3 - 1/2x
y <= 1 - x

最佳答案

如果您从点 (0, 0) 开始绘图,因此不需要考虑不在第一象限中的多边形区域,那么在这种特殊情况下应该可以解决问题:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0,10,0.1)

# The lines to plot
y1 = 4 - 2*x
y2 = 3 - 0.5*x
y3 = 1 -x

# The upper edge of polygon (min of lines y1 & y2)
y4 = np.minimum(y1, y2)

# Set y-limit, making neg y-values not show in plot
plt.ylim(0, 5)

# Plotting of lines
plt.plot(x, y1,
         x, y2,
         x, y3)

# Filling between line y3 and line y4
plt.fill_between(x, y3, y4, color='grey', alpha='0.5')
plt.show()

enter image description here

关于python - Matplotlib 在多行之间填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16417496/

相关文章:

Matplotlib:图例显示不正确

Python内存泄漏: do I need to delete?

python - FacetGrid 上的 Seaborn 颜色条用于具有标准化颜色映射的 histplot

python - 将 pandas 数据框的 3 列绘制为热图

python - 为什么我的 Ubuntu 20.04 DEV 机器上安装了多个 Python 版本?

python - 在 Python 中绘制数组

python - boolean 型年

python - python中的慢递归

python - 在全屏python中打开网络浏览器

python - 如何在 Pyplot 中获得平滑的平均曲线