Ask Your Question
0

Add graphs produced by a for loop?

asked 2012-06-08 12:01:40 +0100

anonymous user

Anonymous

updated 2015-01-13 21:16:22 +0100

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")

    show(p1+p2)

I just get all the graphs one after the other.

edit retag flag offensive close merge delete

Comments

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 13:37:38 +0100 )edit

3 Answers

Sort by » oldest newest most voted
2

answered 2012-06-08 12:33:59 +0100

Shashank gravatar image

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

t=var('t')
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")
show(p1+p2)
edit flag offensive delete link more

Comments

Thanks this is a nice consice way of doing it.

Insaneg33k gravatar imageInsaneg33k ( 2012-06-08 13:35:03 +0100 )edit

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

Eviatar Bach gravatar imageEviatar Bach ( 2012-06-08 19:23:12 +0100 )edit
2

answered 2012-06-08 13:17:28 +0100

calc314 gravatar image

updated 2012-06-08 13:17:55 +0100

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

t=var('t')
p1=Graphics()
p2=Graphics()
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")
show(p1+p2)
edit flag offensive delete link more

Comments

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

Insaneg33k gravatar imageInsaneg33k ( 2012-06-08 13:34:14 +0100 )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 13:37:03 +0100 )edit
0

answered 2012-06-08 13:41:07 +0100

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))
sage: p.show(gridlines=True,
...     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)
      sage: p.show(gridlines=(
      ...    [
      ...     (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

Stats

Asked: 2012-06-08 12:01:40 +0100

Seen: 740 times

Last updated: Jun 08 '12