Matplotlib 在多行之间填充 [英] Matplotlib fill between multiple lines

查看:43
本文介绍了Matplotlib 在多行之间填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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:

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

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

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

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

推荐答案

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

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:

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()

这篇关于Matplotlib 在多行之间填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆