Ask Your Question
3

can gridlines be painted at sqrt(2) ?

asked 2011-03-22 03:28:58 -0600

mathematicboy gravatar image

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 flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
3

answered 2011-03-22 04:06:17 -0600

DSM gravatar image

updated 2011-03-22 04:08:17 -0600

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.

edit flag offensive delete link more

Comments

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

kcrisman gravatar imagekcrisman ( 2011-03-22 04:18:39 -0600 )edit

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

Felix Lawrence gravatar imageFelix Lawrence ( 2011-03-22 04:27:40 -0600 )edit

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

Jason Grout gravatar imageJason Grout ( 2011-03-22 17:15:24 -0600 )edit

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

kcrisman gravatar imagekcrisman ( 2011-03-23 02:12:57 -0600 )edit
1

answered 2011-03-22 04:16:06 -0600

kcrisman gravatar image

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.

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: 2011-03-22 03:28:58 -0600

Seen: 264 times

Last updated: Mar 22 '11