First time here? Check out the FAQ!

Ask Your Question
3

can gridlines be painted at sqrt(2) ?

asked 14 years ago

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.

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
3

answered 14 years ago

DSM gravatar image

updated 14 years ago

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.

Preview: (hide)
link

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 ( 14 years ago )

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

Felix Lawrence gravatar imageFelix Lawrence ( 14 years ago )

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 ( 14 years ago )

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

kcrisman gravatar imagekcrisman ( 14 years ago )
1

answered 14 years ago

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.

Preview: (hide)
link

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: 14 years ago

Seen: 593 times

Last updated: Mar 22 '11