用阴影填充绘制不平等的最简单方法是什么? [英] Easiest way to plot inequalities with hatched fill?

查看:153
本文介绍了用阴影填充绘制不平等的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



参考上面的情节。我已经在excel中绘制了方程,然后用手绘制了阴影。你可以看到它不是很整齐。你可以看到有六个区域,每个区域都由两个或更多方程组成。使用孵化模式绘制不平等并遮蔽区域的最简单方法是什么?

解决方案

建立在@ agstudy的答案上,以下是一种快速且肮脏的方式来表示R中的不等式:

  plot(NA,xlim = c(0,1),ylim = c(0,1),xaxs =i,yaxs =i)#空曲线
a < - curve(x ^ 2,add = TRUE)#第一曲线
b < - 曲线(2 * x ^ 2-0.2,add = TRUE)#第二曲线
名称(a)< -c('xA','yA')
名称(b)< -c('xB','yB')
with as.list(c(b,a)),{
id< -yB< = yA
#b< a area
polygon(x = c(xB [id] (xA [id])),
y = c(yB [id],rev(yA [id])),
density = 10,angle = 0,border = NULL)
# (xB [!id],rev(xA [!id])),
y = c(yB [!id],rev(yA [!id] )),
density = 10,angle = 90,border = NULL)
})



如果有问题的地区是苏由两个以上的方程组成,只需添加更多条件:

$ p $ plot(NA,xlim = c(0,1),ylim =第一曲线
b < - 曲线(x ^ 2,add = TRUE)= c(0,1),xaxs =i,yaxs =i曲线(2 * x ^ 2-0.2,add = TRUE)#第二曲线
d < - 曲线(0.5 * x ^ 2 + 0.2,add = TRUE)#第三曲线

名称(a)< -c('xA','yA')
名称(b)< -c('xB','yB')
名称(c(a,b,d)),{
#'基本上你有三个条件:
#curve a低于曲线b,曲线b低于曲线d,曲线d高于曲线a
#赋值给每条曲线,以协调涉及它的两个条件。

idA< - yA< = yD& yA <= yB
idB <-yB> = yA& yB <= yD
idD < - yD <= yB& yD> = yA
多边形(x = c(xB [idB],xD [idD],rev(xA [idA])),
y = c(yB [idB],yD [idD] rev(yA [idA])),
density = 10,angle = 0,border = NULL)
})


Refer to the above plot. I have drawn the equations in excel and then shaded by hand. You can see it is not very neat. You can see there are six zones, each bounded by two or more equations. What is the easiest way to draw inequalities and shade the regions using hatched patterns ?

解决方案

To build up on @agstudy's answer, here's a quick-and-dirty way to represent inequalities in R:

plot(NA,xlim=c(0,1),ylim=c(0,1), xaxs="i",yaxs="i") # Empty plot
a <- curve(x^2, add = TRUE) # First curve
b <- curve(2*x^2-0.2, add = TRUE) # Second curve
names(a) <- c('xA','yA')
names(b) <- c('xB','yB')
with(as.list(c(b,a)),{
    id <- yB<=yA
    # b<a area
    polygon(x = c(xB[id], rev(xA[id])),
            y = c(yB[id], rev(yA[id])), 
            density=10, angle=0, border=NULL)
    # a>b area
    polygon(x = c(xB[!id], rev(xA[!id])),
            y = c(yB[!id], rev(yA[!id])), 
            density=10, angle=90, border=NULL)
    })

If the area in question is surrounded by more than 2 equations, just add more conditions:

plot(NA,xlim=c(0,1),ylim=c(0,1), xaxs="i",yaxs="i") # Empty plot
a <- curve(x^2, add = TRUE) # First curve
b <- curve(2*x^2-0.2, add = TRUE) # Second curve
d <- curve(0.5*x^2+0.2, add = TRUE) # Third curve

names(a) <- c('xA','yA')
names(b) <- c('xB','yB')
names(d) <- c('xD','yD')

with(as.list(c(a,b,d)),{
    # Basically you have three conditions: 
    # curve a is below curve b, curve b is below curve d and curve d is above curve a
    # assign to each curve coordinates the two conditions that concerns it.

    idA <- yA<=yD & yA<=yB
    idB <- yB>=yA & yB<=yD
    idD <- yD<=yB & yD>=yA
    polygon(x = c(xB[idB], xD[idD], rev(xA[idA])),
            y = c(yB[idB], yD[idD], rev(yA[idA])), 
            density=10, angle=0, border=NULL)
    })

这篇关于用阴影填充绘制不平等的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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