Ask Your Question

Add graphs produced by a for loop?

asked 2012-06-08 05:01:40 -0500

anonymous user


updated 2015-01-13 14:16:22 -0500

FrédéricC gravatar image

How can I plot all my graphs that i produce from a for loop on top of each other? I have this:

for i in range(-3,3,1):

    p1 = parametric_plot((t,i),(t,-5,5),color="red")

    p2 = parametric_plot((i,t),(t,-5,5),color="gray")


I just get all the graphs one after the other.

edit retag flag offensive close merge delete


By the way, you can do `for i in [-3..2]` if you are looking for a more intuitive way to write that range.

kcrisman gravatar imagekcrisman ( 2012-06-08 06:37:38 -0500 )edit

3 answers

Sort by » oldest newest most voted

answered 2012-06-08 05:33:59 -0500

Shashank gravatar image

Try this. Use p1=p1+ ... instead of p1 = ...

for i in range(-3,3,1):
    p1 = p1+parametric_plot((t,i),(t,-5,5),color="red")
    p2 = p2+parametric_plot((i,t),(t,-5,5),color="gray")
edit flag offensive delete link more


Thanks this is a nice consice way of doing it.

Insaneg33k gravatar imageInsaneg33k ( 2012-06-08 06:35:03 -0500 )edit

You can also replace `p1 = p1+...` to `p1 += ...` This just means "add to itself".

Eviatar Bach gravatar imageEviatar Bach ( 2012-06-08 12:23:12 -0500 )edit

answered 2012-06-08 06:17:28 -0500

calc314 gravatar image

updated 2012-06-08 06:17:55 -0500

You'll also need to use Graphics() prior to your for loop to define p1 and p2 to be graphics objects.

for i in range(-3,3,1):
    p1 +=parametric_plot((t,i),(t,-5,5),color="red")
    p2 +=parametric_plot((i,t),(t,-5,5),color="gray")
edit flag offensive delete link more


Thanks, thats exactly what i was looking for! You have pointed me in the right direction! :)

Insaneg33k gravatar imageInsaneg33k ( 2012-06-08 06:34:14 -0500 )edit

Or you could make the first one `i=-3` be outside the loop and then use the `+=` construction in the rest of the loop (which would then be -2 to 3).

kcrisman gravatar imagekcrisman ( 2012-06-08 06:37:03 -0500 )edit

answered 2012-06-08 06:41:07 -0500

kcrisman gravatar image

Just on the off chance that you are trying to just make gridlines, here are some examples from the documentation:

sage: p = polar_plot(2 + 2*cos(x), 0, 2*pi, color=hue(0.3))
...     hgridlinesstyle=dict(color="orange", linewidth=1.0),
...     vgridlinesstyle=dict(color="blue", linestyle=":"))

   Change the style of each grid line individually.

      sage: x, y = var('x, y')
      sage: p = implicit_plot((y^2-x^2)*(x-1)*(2*x-3)-4*(x^2+y^2-2*x)^2,
      ...             (x,-2,2), (y,-2,2), plot_points=1000)
      ...    [
      ...     (1,{"color":"red","linestyle":":"}),
      ...     (0,{"color":"blue","linestyle":"--"})
      ...    ],
      ...    [
      ...     (-1,{"color":"red","linestyle":":"}),
      ...     (0,{"color":"blue","linestyle":"--"}),
      ...     (1,{"color":"red","linestyle":":"}),
      ...    ]
      ...    ),
      ...    gridlinesstyle=dict(marker='x',color="black"))

There are pretty finely-grained options available.

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


Asked: 2012-06-08 05:01:40 -0500

Seen: 173 times

Last updated: Jun 08 '12