ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 14 Sep 2018 14:04:46 +0200Why are my plots printing out of order?https://ask.sagemath.org/question/43666/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[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.Fri, 14 Sep 2018 07:11:37 +0200https://ask.sagemath.org/question/43666/why-are-my-plots-printing-out-of-order/Comment by kcrisman for <p>Here is my code:</p>
<pre><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)
</code></pre>
<p>I know that the plots are printing correctly, they are just out of order for some reason. Any ideas?</p>
<p>Also, I know this is a terrible way to plot this. I wanted to use:</p>
<pre><code>plot(f(I[0],I[1],x),(x,0,1))
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/43666/why-are-my-plots-printing-out-of-order/?comment=43669#post-id-43669For 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.Fri, 14 Sep 2018 14:04:46 +0200https://ask.sagemath.org/question/43666/why-are-my-plots-printing-out-of-order/?comment=43669#post-id-43669Answer by eric_g for <p>Here is my code:</p>
<pre><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)
</code></pre>
<p>I know that the plots are printing correctly, they are just out of order for some reason. Any ideas?</p>
<p>Also, I know this is a terrible way to plot this. I wanted to use:</p>
<pre><code>plot(f(I[0],I[1],x),(x,0,1))
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/43666/why-are-my-plots-printing-out-of-order/?answer=43668#post-id-43668This 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`. Fri, 14 Sep 2018 13:27:48 +0200https://ask.sagemath.org/question/43666/why-are-my-plots-printing-out-of-order/?answer=43668#post-id-43668