Ask Your Question
2

A bug in graphics.py

asked 2012-06-25 11:00:49 +0200

updated 2023-01-10 00:01:07 +0200

tmonteil gravatar image

Several hours ago I post my question about unicode support in "axes_labels",here is my question

I am sure this is really a bug of sage,I don't know how to report this bug as the spreadsheets.google.com is block by Chinese government.

As we know that Matplotlib supports unicode with some setting in "matplotlibrc",and I make it work with Chinese characters now.But when I want to set the axes labels to some Chinese characters with "axes_labels" attribute ,I got exception:"UnicodeEncodeError: 'ascii' codec can't encode character u'\u6211' in position 0: ordinal not in range(128)"

After I changed some code in sage/local/lib/python/site.py,I set the default encoding to "utf-8",that exception never raise again.But the problem does not be solved as the Chinese characters shown in the axes labels all become boxs like [].

Why I can set the axes labels in Chinese with matplotlib but can not in sage? After I check the source code in

SAGE/local/lib/python2.7/site-packages/sage/plot/graphics.py

In line 634 of the method "def axes_labels(self, l=None)",I found the line is

self.__axes_labels = (str(l[0]), str(l[1]))

It is obviously that the problem is caused by the str(),after change this line to

self.__axes_labels = (l[0], l[1])

and remove the SAGE/local/lib/python2.7/site-packages/sage/plot/graphics.pyc,restart sage , I finally got this problem solved.Now Sage gets no problem to work with Chinese,or in other word,gets no problem to work with unicode(utf-8).

There is some work to do with matplotlibrc,it is in sage/local/lib/python/site-packages/matplotlib/mpl-data

First,we need to download one free unicode font,more information can refer to WikiPedia,and I use Bitstream Cyberbit for a test.I think there are more better unicode fonts.Bitstream Cyberbit can work well with Chinese,Japanese,Korean in my test.

After unzip the downloaded file,get the Cyberbit.ttf and put it into sage/local/lib/python/site-packages/matplotlib/mpl-data/fonts/ttf.And then open the matplotlibrc,remove the "#" which at the begin of

font.family : sans-serif

font.style : normal

font.variant : normal

font.weight : medium

font.stretch : normal

It seems that only sans-serif and serif can work well with unicode.

And then ,remove the "#" at the begin of "font.sans-serif " in line 129,add "Bitstream Cyberbit" after ":",it must put on the first,like:

font.sans-serif : Bitstream Cyberbit,Microsoft YaHei,Bitstream Vera Sans, Lucida Grande

It also needs to remove the "#" in the begin of axes.unicode_minus,and set the boolean value to "False",for example:

axes.unicode_minus : False

Save this file and restart Sage,know we can test it with:

plot(x,axes_labels=['z',u'???????'])

It seems that this website does not support unicode.Don't forget the "u" .

The Bitstream Cyberbit,though work well with unicode,I think it is not so beautiful.Maybe there is better one.

It ... (more)

edit retag flag offensive close merge delete

Comments

An unrelated, but useful, comment; "and remove the SAGE/local/lib/python2.7/site-packages/sage/plot/graphics.pyc,restart sage" is not necessary; if you just restart sage with the `sage -b` flag it should make the changes you made in `graphics.py` live.

kcrisman gravatar imagekcrisman ( 2012-06-25 12:27:35 +0200 )edit

I see that you post a problem in the "Trac 13161" of Korean , the problem is the same with me as matplotlib come with some fonts only support ASCII. So I think you should put more free fonts in matplotlib and make some change in matplotlibrc

shahuwang gravatar imageshahuwang ( 2012-06-25 13:35:40 +0200 )edit

Ah, so you are saying that we would need to include additional fonts?

kcrisman gravatar imagekcrisman ( 2012-06-25 15:41:10 +0200 )edit

I update my question with my solution posted.

shahuwang gravatar imageshahuwang ( 2012-06-26 10:28:11 +0200 )edit

@shahuwang: Hey, this Trac ticket has positive review now. I would love to be able to add your name as an author - and hence newest contributor to Sage! See http://trac.sagemath.org/sage_trac/ticket/13161#comment:11

kcrisman gravatar imagekcrisman ( 2012-07-26 13:19:13 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2012-06-25 12:38:31 +0200

kcrisman gravatar image

This seems to still be try in Sage 5.1.beta5. I've opened Trac 13161 for this. If you get an account there, you can even upload a patch (see the developer guide) and we can give you author credit!

I'm wondering if that is the whole solution, by the way; we don't really want end users to need to do something with their rc files just to plot axes labels.

edit flag offensive delete link more

Comments

It takes some time to set the matplotlibrc file,as it come with no font that support Chinese.It seems that there are only fonts that support ASCII .I wish next released to get the matplotlibrc setted to support unicode and with some free fonts to support non-ASCII used countries such as China,Japan. Wish to help more

shahuwang gravatar imageshahuwang ( 2012-06-25 13:25:02 +0200 )edit
kcrisman gravatar imagekcrisman ( 2012-07-17 12:30:06 +0200 )edit

This ticket now has positive review.

kcrisman gravatar imagekcrisman ( 2012-07-26 13:19:34 +0200 )edit

I see it,thanks to review it,wish I can help more

shahuwang gravatar imageshahuwang ( 2012-07-31 11:01:10 +0200 )edit

Please do, by giving us your *real name* so that we can properly acknowledge you as one of the authors!

kcrisman gravatar imagekcrisman ( 2012-07-31 14:28:08 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2012-06-25 11:00:49 +0200

Seen: 882 times

Last updated: Jun 26 '12