# Why are my plots printing out of order?

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,I,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,I,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 close merge delete

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.

Sort by » oldest newest most voted

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

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


Otherwise f(I,I,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.

more