Ask Your Question
1

How to plot many functions on the same graph

asked 2012-08-08 05:10:57 +0100

Aeol Kong gravatar image

updated 2015-01-18 18:13:06 +0100

FrédéricC gravatar image

Hello, I have a list of functions but I don't know how to put them all on a single graph? I know one way to use 'show' like : show(plot1+plot2),

but is there a way that can cope with a function list or graph obj list? As maybe I'm not sure how many functions to plot.

for a specific exapmle:

var('x y')
f=x^0.5+y
lfs=[]
for i in range(5):
    fs.append(f-i)

now how to show all functions in the lfs list on a single graph?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
2

answered 2012-08-08 08:40:03 +0100

calc314 gravatar image

That is it. You can also use the sum command to combine a list of plot objects.

f=x^2+10
p=[]
for i in range(0,5):
    p.append(plot(f-i,(x,-3,3)))
sum(p)

You can use list comprehensions in Python to shorten this significantly and produce the same result.

sum([plot(x^2+10 - i,(x,-3,3)) for i in range(0,5)])
edit flag offensive delete link more

Comments

Doesn't seem to work. Complains about wrong type. Apparently you can't add plots together with sum(). Leaky abstraction?

TypeError: unsupported operand type(s) for +: 'int' and 'tuple'

Rulatir gravatar imageRulatir ( 2022-10-31 00:25:35 +0100 )edit
2

answered 2012-08-08 05:30:10 +0100

Aeol Kong gravatar image

I've got a solution like this:

var('x')
g = Graphics()
g += plot(x-3, (x,-5,5))
g += plot(x-2, (x, -5, 5))
g.show()
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

Stats

Asked: 2012-08-08 05:10:57 +0100

Seen: 20,521 times

Last updated: Aug 08 '12