ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 23 Mar 2011 02:12:57 -0500can gridlines be painted at sqrt(2) ?http://ask.sagemath.org/question/8018/can-gridlines-be-painted-at-sqrt2/In this simple plot
plot(x,0,2,gridlines=([1],[]))
the gridline is plotted alright. However, if I put sqrt(2) instead of 1
plot(x,0,2,gridlines=([sqrt(2)],[]))
does not work. It is strange, because I think that gridlines should behave similar as ticks. For instance, the following both two expressions work
plot(x,0,2,ticks=([sqrt(2)],[]))
plot(x,0,2,ticks=([sqrt(2)],[]),gridlines=true)
Does anybody know what is the reason or how to fix it?
Thanks.
Javier Pérez.Tue, 22 Mar 2011 03:28:58 -0500http://ask.sagemath.org/question/8018/can-gridlines-be-painted-at-sqrt2/Answer by DSM for <p>In this simple plot</p>
<p>plot(x,0,2,gridlines=([1],[]))</p>
<p>the gridline is plotted alright. However, if I put sqrt(2) instead of 1</p>
<p>plot(x,0,2,gridlines=([sqrt(2)],[]))</p>
<p>does not work. It is strange, because I think that gridlines should behave similar as ticks. For instance, the following both two expressions work</p>
<p>plot(x,0,2,ticks=([sqrt(2)],[]))
plot(x,0,2,ticks=([sqrt(2)],[]),gridlines=true)</p>
<p>Does anybody know what is the reason or how to fix it?
Thanks.
Javier Pérez.</p>
http://ask.sagemath.org/question/8018/can-gridlines-be-painted-at-sqrt2/?answer=12218#post-id-12218You can work around this by replacing sqrt(2) with RR(sqrt(2)) or sqrt(2).n() or float(sqrt(2)) -- anything which turns it from an Expression into a (real) number.
I think this is a bug caused by the quirk that matplotlib thinks that Sage Expressions are iterable because it tests whether "len" works:
def iterable(obj):
'return true if *obj* is iterable'
try: len(obj)
except: return False
return True
and sqrt(2) *does* have a working len, even though it's not iterable:
sage: len(sqrt(2))
2
sage: list(sqrt(2))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
[...]
TypeError: 'sage.symbolic.expression.Expression' object is not iterable
I've confirmed that if I hack matplotlib.cbook.iterable it works. Probably the right way to fix this is to get plot to coerce to floats at the start using to_float_list, which strangely enough isn't currently used anywhere in sage/plot.Tue, 22 Mar 2011 04:06:17 -0500http://ask.sagemath.org/question/8018/can-gridlines-be-painted-at-sqrt2/?answer=12218#post-id-12218Comment by kcrisman for <p>You can work around this by replacing sqrt(2) with RR(sqrt(2)) or sqrt(2).n() or float(sqrt(2)) -- anything which turns it from an Expression into a (real) number.</p>
<p>I think this is a bug caused by the quirk that matplotlib thinks that Sage Expressions are iterable because it tests whether "len" works:</p>
<pre><code>def iterable(obj):
'return true if *obj* is iterable'
try: len(obj)
except: return False
return True
</code></pre>
<p>and sqrt(2) <em>does</em> have a working len, even though it's not iterable:</p>
<pre><code>sage: len(sqrt(2))
2
sage: list(sqrt(2))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
[...]
TypeError: 'sage.symbolic.expression.Expression' object is not iterable
</code></pre>
<p>I've confirmed that if I hack matplotlib.cbook.iterable it works. Probably the right way to fix this is to get plot to coerce to floats at the start using to_float_list, which strangely enough isn't currently used anywhere in sage/plot.</p>
http://ask.sagemath.org/question/8018/can-gridlines-be-painted-at-sqrt2/?comment=21951#post-id-21951Nice catch on why mpl thinks they are iterable - I was wondering about that, but didn't have time to investigate. Tue, 22 Mar 2011 04:18:39 -0500http://ask.sagemath.org/question/8018/can-gridlines-be-painted-at-sqrt2/?comment=21951#post-id-21951Comment by Jason Grout for <p>You can work around this by replacing sqrt(2) with RR(sqrt(2)) or sqrt(2).n() or float(sqrt(2)) -- anything which turns it from an Expression into a (real) number.</p>
<p>I think this is a bug caused by the quirk that matplotlib thinks that Sage Expressions are iterable because it tests whether "len" works:</p>
<pre><code>def iterable(obj):
'return true if *obj* is iterable'
try: len(obj)
except: return False
return True
</code></pre>
<p>and sqrt(2) <em>does</em> have a working len, even though it's not iterable:</p>
<pre><code>sage: len(sqrt(2))
2
sage: list(sqrt(2))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
[...]
TypeError: 'sage.symbolic.expression.Expression' object is not iterable
</code></pre>
<p>I've confirmed that if I hack matplotlib.cbook.iterable it works. Probably the right way to fix this is to get plot to coerce to floats at the start using to_float_list, which strangely enough isn't currently used anywhere in sage/plot.</p>
http://ask.sagemath.org/question/8018/can-gridlines-be-painted-at-sqrt2/?comment=21947#post-id-21947I think matplotlib should check for iterability by either: calling iter(obj) and checking for a TypeError, or doing isinstance(obj, collections.Iterable).Tue, 22 Mar 2011 17:15:24 -0500http://ask.sagemath.org/question/8018/can-gridlines-be-painted-at-sqrt2/?comment=21947#post-id-21947Comment by kcrisman for <p>You can work around this by replacing sqrt(2) with RR(sqrt(2)) or sqrt(2).n() or float(sqrt(2)) -- anything which turns it from an Expression into a (real) number.</p>
<p>I think this is a bug caused by the quirk that matplotlib thinks that Sage Expressions are iterable because it tests whether "len" works:</p>
<pre><code>def iterable(obj):
'return true if *obj* is iterable'
try: len(obj)
except: return False
return True
</code></pre>
<p>and sqrt(2) <em>does</em> have a working len, even though it's not iterable:</p>
<pre><code>sage: len(sqrt(2))
2
sage: list(sqrt(2))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
[...]
TypeError: 'sage.symbolic.expression.Expression' object is not iterable
</code></pre>
<p>I've confirmed that if I hack matplotlib.cbook.iterable it works. Probably the right way to fix this is to get plot to coerce to floats at the start using to_float_list, which strangely enough isn't currently used anywhere in sage/plot.</p>
http://ask.sagemath.org/question/8018/can-gridlines-be-painted-at-sqrt2/?comment=21946#post-id-21946You wanna report that upstream, then? I've edited #10980 to include that info.Wed, 23 Mar 2011 02:12:57 -0500http://ask.sagemath.org/question/8018/can-gridlines-be-painted-at-sqrt2/?comment=21946#post-id-21946Comment by Felix Lawrence for <p>You can work around this by replacing sqrt(2) with RR(sqrt(2)) or sqrt(2).n() or float(sqrt(2)) -- anything which turns it from an Expression into a (real) number.</p>
<p>I think this is a bug caused by the quirk that matplotlib thinks that Sage Expressions are iterable because it tests whether "len" works:</p>
<pre><code>def iterable(obj):
'return true if *obj* is iterable'
try: len(obj)
except: return False
return True
</code></pre>
<p>and sqrt(2) <em>does</em> have a working len, even though it's not iterable:</p>
<pre><code>sage: len(sqrt(2))
2
sage: list(sqrt(2))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
[...]
TypeError: 'sage.symbolic.expression.Expression' object is not iterable
</code></pre>
<p>I've confirmed that if I hack matplotlib.cbook.iterable it works. Probably the right way to fix this is to get plot to coerce to floats at the start using to_float_list, which strangely enough isn't currently used anywhere in sage/plot.</p>
http://ask.sagemath.org/question/8018/can-gridlines-be-painted-at-sqrt2/?comment=21950#post-id-21950Don't forget the most compact option: sqrt(2.)Tue, 22 Mar 2011 04:27:40 -0500http://ask.sagemath.org/question/8018/can-gridlines-be-painted-at-sqrt2/?comment=21950#post-id-21950Answer by kcrisman for <p>In this simple plot</p>
<p>plot(x,0,2,gridlines=([1],[]))</p>
<p>the gridline is plotted alright. However, if I put sqrt(2) instead of 1</p>
<p>plot(x,0,2,gridlines=([sqrt(2)],[]))</p>
<p>does not work. It is strange, because I think that gridlines should behave similar as ticks. For instance, the following both two expressions work</p>
<p>plot(x,0,2,ticks=([sqrt(2)],[]))
plot(x,0,2,ticks=([sqrt(2)],[]),gridlines=true)</p>
<p>Does anybody know what is the reason or how to fix it?
Thanks.
Javier Pérez.</p>
http://ask.sagemath.org/question/8018/can-gridlines-be-painted-at-sqrt2/?answer=12219#post-id-12219The problem is that the gridlines are looking for something that is only a certain type of number, not a symbolic expression.
sage: plot(x,0,2,gridlines=[[1.4],[]])
works, for instance, as would
sage: plot(x,0,2,gridlines=[[sqrt(2).n()],[]])
That's what I'd recommend as a workaround for now.
The real problem is that the code
else:
vl=vline
st=vgridstyle
subplot.axvline(vl,**st)
is sending a symbolic expression to matplotlib as vl, which it cannot handle. But anything that has a real `n()` method should be legitimate input.
This is now [#10980](http://trac.sagemath.org/sage_trac/ticket/10980).Tue, 22 Mar 2011 04:16:06 -0500http://ask.sagemath.org/question/8018/can-gridlines-be-painted-at-sqrt2/?answer=12219#post-id-12219