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

查看:123
本文介绍了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天全站免登陆