# can gridlines be painted at sqrt(2) ?

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.

edit retag close merge delete

Sort by » oldest newest most voted

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.

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.

more

Nice catch on why mpl thinks they are iterable - I was wondering about that, but didn't have time to investigate.

( 2011-03-22 10:18:39 +0200 )edit

Don't forget the most compact option: sqrt(2.)

( 2011-03-22 10:27:40 +0200 )edit

I think matplotlib should check for iterability by either: calling iter(obj) and checking for a TypeError, or doing isinstance(obj, collections.Iterable).

( 2011-03-22 23:15:24 +0200 )edit

You wanna report that upstream, then? I've edited #10980 to include that info.

( 2011-03-23 08:12:57 +0200 )edit

The 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.

more