Ask Your Question

How do I plot multiple functions in one graph?

asked 2021-08-21 19:11:31 +0100

ivaralink gravatar image

updated 2021-08-21 22:06:42 +0100

dsejas gravatar image

What I do:

x, y = SR.var('x, y')
eq1 = 2*x + y == 8
eq2 = 5*x -7*y == 1

What I get:

TypeError: 'tuple' object is not callable

What am I doing wrong?

edit retag flag offensive close merge delete


The best i can find is:

show(implicit_plot(eq1,(x,-2,5),(y,-2,5)) + implicit_plot(eq2,(x,-2,5),(y,-2,5)))

But it sould be possible to do it shorter, should't it?

ivaralink gravatar imageivaralink ( 2021-08-21 19:25:21 +0100 )edit

Since plot can plot either a function or a list of functions, it is indeed a bit surprising that implicit_plot only accepts one equation, and not a list of equations. But that's how it is at this point!

slelievre gravatar imageslelievre ( 2021-08-22 02:44:26 +0100 )edit

Strange as it may seem , i checked the documentation and the parameters correspond to the documentation. Strange but correct, there probably is a reason.

ivaralink gravatar imageivaralink ( 2021-08-22 13:58:24 +0100 )edit

Do you mean this example is from the documentation and it fails? Can you say what page?

slelievre gravatar imageslelievre ( 2021-08-22 23:51:42 +0100 )edit

Bi, i mean that according to the documentation plot takes a collection of objects and implicit_plot only takes a function. The difference is strange

ivaralink gravatar imageivaralink ( 2021-08-23 13:39:27 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2021-08-21 21:20:17 +0100

dsejas gravatar image

updated 2021-08-21 21:22:34 +0100

Hello, @ivaralink! Indeed, the solution you pose in your own comment is correct, albeit not stylistically elegant. Superimposing plots can be achieved by "adding" them. However, doing so in one command is considered a poor programming practice. I would recommend doing the following:

P1 = implicit_plot(eq1, (x,-2,5), (y,-2,5))
P2 = implicit_plot(eq2, (x,-2,5), (y,-2,5))
show(P1 + P2)

Even better, you can use the += composite operator:

P = implicit_plot(eq1, (x,-2,5), (y,-2,5))
P += implicit_plot(eq2, (x,-2,5), (y,-2,5))

(Alternatively, this last line can also be show(P).)

I recommend you read the 2D plotting documentation. Also, if you are beginner, I also recommend you read this book by Gregory Bard, which can be downloaded for free..

I hope this helps!

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


Asked: 2021-08-21 19:11:31 +0100

Seen: 561 times

Last updated: Aug 21 '21