First time here? Check out the FAQ!

Ask Your Question
1

How to color area between 2+ functions

asked 1 year ago

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.

Preview: (hide)

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 ( 1 year ago )

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

Thank you very much!

Alex89 gravatar imageAlex89 ( 1 year ago )

2 Answers

Sort by » oldest newest most voted
1

answered 1 year ago

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
Preview: (hide)
link

Comments

Looks great! Thank you.

Alex89 gravatar imageAlex89 ( 1 year ago )
1

answered 1 year ago

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

Preview: (hide)
link

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: 1 year ago

Seen: 247 times

Last updated: Jul 25 '23