Ask Your Question
1

plotting multiple functions from a for loop

asked 2019-04-25 04:06:52 -0500

thetha gravatar image

updated 2019-04-25 05:07:23 -0500

This solution: https://ask.sagemath.org/question/902... is not working any more

The error is:

Error in lines 3-3
Traceback (most recent call last):
  File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute
    flags=compile_flags) in namespace, locals
  File "", line 1, in <module>
  File "/ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/sage/misc/decorators.py", line 492, in wrapper
    return func(*args, **options)
  File "/ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/sage/plot/plot.py", line 1956, in plot
    G = _plot(funcs, (xmin, xmax), **kwds)
  File "/ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/sage/plot/plot.py", line 2224, in _plot
    legend_label=legend_label_entry, legend_color=legend_color_entry, **options_temp)
  File "/ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/sage/misc/decorators.py", line 492, in wrapper
    return func(*args, **options)
  File "/ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/sage/plot/plot.py", line 1941, in plot
    G = funcs.plot(*args, **original_opts)
TypeError: plot() takes exactly 1 argument (17 given)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-04-25 12:33:43 -0500

dsejas gravatar image

Hello, @thetha. The problem is that

plotlist = [f.plot((x, 1, 10)) for f in funclist]

defines a list of Sage plots. You can verify this by writing

type(plotlist)
type(plotlist[0])

You should get:

<type 'list'>
<class 'sage.plot.graphics.Graphics'>

What you need is just one Sage plot, i.e., a <class 'sage.plot.graphics.Graphics'>, like any of the plotlist[i].

The solution is the following:

funclist = [1/x^i for i in range(10)]
plotlist = sum(f.plot((x, 1, 10)) for f in funclist)
plot(plotlist)

This way, instead of creating a list of plot, you sum all the plots together, which is equivalent to superimpose them in a single image. You can verify this by typing again:

type(plotlist)

(This time you will get <class 'sage.plot.graphics.Graphics'>, as desired.)

The result should look like this: image description

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: 2019-04-25 04:06:52 -0500

Seen: 54 times

Last updated: Apr 25