Ask Your Question
1

Why are my plots printing out of order?

asked 2018-09-14 07:11:37 +0100

nbenj582 gravatar image

Here is my code:

def lam(n):
    m = 1
    while (n+1 - m >= 0):
        m = 2*m
    return m/2

def inter(n,l):
    return [(n+1-l)/l, (n+2 - l)/l]

def f(a,b,x):
    if x>=a and x<=b:
        return 1
    else:
        return 0

for n in range(1,15):
    I = inter(n,lam(n))
    lst = []
    for i in range(101):
        lst.append([i/100, f(I[0],I[1],i/100)])
    scatter_plot(lst,markersize=20,aspect_ratio=0.1)

I know that the plots are printing correctly, they are just out of order for some reason. Any ideas?

Also, I know this is a terrible way to plot this. I wanted to use:

plot(f(I[0],I[1],x),(x,0,1))

but that doesn't seem to be working (it just gives me the zero function), so if you have any suggestions for that as well, that would be great. Thank you.

edit retag flag offensive close merge delete

Comments

1

For reference, can you mention what interface (command-line, sagenb, Jupyter notebook, CoCalc Sage worksheet) you are using? It's conceivable the answer may be different depending on this.

kcrisman gravatar imagekcrisman ( 2018-09-14 14:04:46 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-09-14 13:27:48 +0100

eric_g gravatar image

This is an answer to the second part of the question only: you should use

plot(lambda x: f(I[0],I[1],x), (x,0,1))

Otherwise f(I[0],I[1],x) gets first evaluated with the symbolic variable x and returns the value zero (because the test x>=a and x<=b in f always fails with the symbolic x); this zero value is then passed to plot.

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: 2018-09-14 07:11:37 +0100

Seen: 253 times

Last updated: Sep 14 '18