# How to color area between 2+ functions

y1 =(4-x)/2
y2 = 2 - 2*x
y3 = (-3+x)/2

plot([y1,y2,y3], (x,-1,6), ymax=10, ymin=-4,gridlines='minor')


I want to colorize the triangle formed by these 3 linear functions. How can I do that? fill = True just confuses the graph.

edit retag close merge delete

1

Do you want to do this just for linear functions? In that case I would just determine the intersection points and draw the triangle as a polygon.

( 2023-07-24 22:16:33 +0100 )edit

I'm studying linear programming and this is just perfect! Exactly what I was looking for.

Thank you very much!

( 2023-07-25 11:14:29 +0100 )edit

Sort by ยป oldest newest most voted

You can use region_plot(documentation is on the page https://doc.sagemath.org/html/en/refe..., but I don't see how to link directly to that particular part of the page).

sage: var('y')
y
sage: region_plot([y < y1, y > y2, y > y3], (x, -1, 6), (y, -4, 10))


will produce a filled triangle. To combine with the previous plot, add them:

sage: P = plot([y1,y2,y3], (x,-1,6), ymax=10, ymin=-4,gridlines='minor')
sage: Q = region_plot([y < y1, y > y2, y > y3], (x, -1, 6), (y, -4, 10))
sage: P + Q

more

Looks great! Thank you.

( 2023-07-25 11:16:02 +0100 )edit

See "The basic options for filling a list of plots" and the surrounding examples in

and the various other examples on that page.

Scroll through that whole richly illustrated documentation page.

more