Ask Your Question
1

How to color area between 2+ functions

asked 2023-07-24 10:17:13 +0100

Alex89 gravatar image
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 flag offensive close merge delete

Comments

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.

rburing gravatar imagerburing ( 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!

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

2 Answers

Sort by ยป oldest newest most voted
1

answered 2023-07-24 22:20:07 +0100

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
edit flag offensive delete link more

Comments

Looks great! Thank you.

Alex89 gravatar imageAlex89 ( 2023-07-25 11:16:02 +0100 )edit
1

answered 2023-07-25 09:02:58 +0100

slelievre gravatar image

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.

See also other examples at

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-07-24 10:17:13 +0100

Seen: 167 times

Last updated: Jul 25 '23