Python Matplotlib 在多行之间填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16417496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:33:43  来源:igfitidea点击:

Matplotlib fill between multiple lines

pythonmatplotlib

提问by Magnar

I would like to fill between 3 lines in matplotlib.pyplot but unfortunately the fill_betweengives me opportunity to fill between only two lines. Any ideas how to deal with this?

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

Edit:

编辑:

Ok, I did not explain what I really mean since I cannot add the picture with my current reputation so maybe in that way:

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

I try to fill the polygon bounded by these lines and I have no idea how because fill_between gives me opportunity to fill only area between two of them. Below the fill equation:

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

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

the x and y bigger than 0 is obvious. I start the plot from (0,0) but I still have 3 lines...

x 和 y 大于 0 是显而易见的。我从 (0,0) 开始绘图,但我仍然有 3 行...

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

采纳答案by sodd

If you start the plot in point (0, 0), and therefore do not need to consider the area of the polygon not in the first quadrant, then this should do the trick in this particular situation:

如果您从 (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

在此处输入图片说明

回答by Hooked

To use fill_between, specify the X values first, than the two Y sets that you want to "fill between". An example is show below:

要使用fill_between,请先指定 X 值,而不是要“填充”的两个 Y 集。一个例子如下所示:

import pylab as plt
import numpy as np

X  = np.linspace(0,3,200)
Y1 = X**2 + 3
Y2 = np.exp(X) + 2
Y3 = np.cos(X)

plt.plot(X,Y1,lw=4)
plt.plot(X,Y2,lw=4)
plt.plot(X,Y3,lw=4)

plt.fill_between(X, Y1,Y2,color='k',alpha=.5)
plt.fill_between(X, Y1,Y3,color='y',alpha=.5)

plt.show()

enter image description here

在此处输入图片说明

If, instead, you only wanted to fill between Y2 and Y3:

相反,如果您只想在 Y2 和 Y3 之间填充:

plt.fill_between(X, Y2,Y3,color='m',alpha=.5)

this would give you

这会给你

enter image description here

在此处输入图片说明

回答by moooeeeep

Just compute the corner points of the polygon, i.e., the points where the lines intersect. Then plot the polygon using pyplot.fill.

只需计算多边形的角点,即线相交的点。然后使用pyplot.fill绘制多边形。

Example:

例子:

import matplotlib.pyplot as plt

# define corner points
x = [1,2,1,0]
y = [2,1,0,1]

# plot
plt.fill(x,y)
plt.show()

resulting Image: Figure 1

结果图像: 图1