# can gridlines be painted at sqrt(2) ?

In this simple plot

plot(x,0,2,gridlines=(,[]))

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.

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

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

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
{"ft_lang_label":"__label__en","ft_lang_prob":0.8604265,"math_prob":0.9257189,"size":502,"snap":"2020-24-2020-29","text_gpt3_token_len":154,"char_repetition_ratio":0.17068273,"word_repetition_ratio":0.0,"special_character_ratio":0.30876493,"punctuation_ratio":0.225,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9814518,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-05-27T03:46:12Z\",\"WARC-Record-ID\":\"<urn:uuid:c88003ac-a7d9-462b-86e8-7611dbaef053>\",\"Content-Length\":\"65831\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:db2577ee-a1cb-496a-8d3a-af44c04b0f5b>\",\"WARC-Concurrent-To\":\"<urn:uuid:8715a555-2207-4957-bc6c-9f0ae6a19aa3>\",\"WARC-IP-Address\":\"140.254.118.68\",\"WARC-Target-URI\":\"https://ask.sagemath.org/question/8018/can-gridlines-be-painted-at-sqrt2/?answer=12218\",\"WARC-Payload-Digest\":\"sha1:JX54ZCKACY6XDFCCRJ6NT5E5UBB35M6Z\",\"WARC-Block-Digest\":\"sha1:2HPWWYJUSIOAJY63CLW6I6MRO4G3MXAZ\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-24/CC-MAIN-2020-24_segments_1590347392057.6_warc_CC-MAIN-20200527013445-20200527043445-00491.warc.gz\"}"}