ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 19 Dec 2019 10:35:23 +0100unicode codinghttps://ask.sagemath.org/question/49080/unicode-coding/```
# -*- coding = UTF-8 -*-
```
does not work ?
Big thanksJingenblThu, 19 Dec 2019 10:35:23 +0100https://ask.sagemath.org/question/49080/UnicodeDecodeError in matplotlib if 'python' set instead of 'sage' in Notebookhttps://ask.sagemath.org/question/37364/unicodedecodeerror-in-matplotlib-if-python-set-instead-of-sage-in-notebook/Hello!
I am trying to make publication ready images for LaTeX in Sage with matplotlib (due to the complex nature of the plot) in Notebook.
I have a lot of numerical calculations (not symbolic) so I switch to 'python-mode' using drop-down list on top of the Worksheet (so it looks like 'File...', 'Action...', 'Data...', 'python') so that my constants would read as native python's data types not Sage's symbolic objects.
Now having the switch on top of the page in 'python' mode the following code:
from matplotlib import rc
rc('text', usetex=True)
rc('text.latex', unicode=True)
rc('text.latex', preamble='\usepackage[utf8]{inputenc}')
rc('text.latex', preamble='\usepackage[russian]{babel}')
font = {'family': 'serif',
'serif': ['Computer Modern Unicode']}
rc('font', **font)
import matplotlib.pyplot as plt
plt.plot([1], [1])
plt.title(ur'Тест')
plt.savefig("test.png")
yields ether UnicodeDecodeError or the image with the text corrupted:
![example of the corrupted output](/upfiles/14928022812226123.png)
But if I switch to 'sage' on top of the page, it works as expected:
![example of the valid output](/upfiles/14928023491980951.png)
I have no idea how that switch on top of the page affects matplotlib's output, but would really like to be able to use Cyrillic (utf8) letters and 'python' mode **at the same time**.
P.S. That magical switch on top of the page is really painful since I also can not save Worksheets if they contain utf-8 characters: [as in this still unresolved issue](https://ask.sagemath.org/question/26167/unicodedecodeerror-in-notebook-server-if-worksheet-is-set-to-python-instead-of-sage/) :-( :-(
**UPDATE:** Tested the MWE above in Jupiter - seems to work properly! And due to inevitable migration to Jupiter, the problem seems not to be obsolete.EugeneFri, 21 Apr 2017 21:24:54 +0200https://ask.sagemath.org/question/37364/UnicodeDecodeError in Notebook Server if Worksheet is set to 'python' instead of 'sage'https://ask.sagemath.org/question/26167/unicodedecodeerror-in-notebook-server-if-worksheet-is-set-to-python-instead-of-sage/Hello!
I have a worksheet with non-ascii characters in the source code (utf-8 comments). If I have 'sage' selected in the combo-box on top of the worksheet then "Save worksheet to a file" works well. But if I change this to 'python' than calling "Save worksheet to a file" leads to a UnicodeDecodeError:
'ascii' codec can't decode byte 0xd0 in position 6: ordinal not in range(128)
Traceback (most recent call last):
File "/home/sage/sage-6.5/local/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/sage/sage-6.5/local/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/sage/sage-6.5/local/lib/python2.7/site-packages/sagenb-0.11.4-py2.7.egg/sagenb/flask_version/decorators.py", line 22, in wrapper
return f(*args, **kwds)
File "/home/sage/sage-6.5/local/lib/python2.7/site-packages/sagenb-0.11.4-py2.7.egg/sagenb/flask_version/worksheet.py", line 48, in wrapper
return f(username, id, **kwds)
File "/home/sage/sage-6.5/local/lib/python2.7/site-packages/sagenb-0.11.4-py2.7.egg/sagenb/flask_version/worksheet.py", line 137, in wrapper
return f(*args, **kwds)
File "/home/sage/sage-6.5/local/lib/python2.7/site-packages/sagenb-0.11.4-py2.7.egg/sagenb/flask_version/worksheet.py", line 938, in worksheet_download
return unconditional_download(worksheet, title)
File "/home/sage/sage-6.5/local/lib/python2.7/site-packages/sagenb-0.11.4-py2.7.egg/sagenb/flask_version/worksheet.py", line 950, in unconditional_download
g.notebook.export_worksheet(worksheet.filename(), filename, title)
File "/home/sage/sage-6.5/local/lib/python2.7/site-packages/sagenb-0.11.4-py2.7.egg/sagenb/notebook/notebook.py", line 748, in export_worksheet
S.export_worksheet(username, id_number, output_filename, title=title)
File "/home/sage/sage-6.5/local/lib/python2.7/site-packages/sagenb-0.11.4-py2.7.egg/sagenb/storage/filesystem_storage.py", line 491, in export_worksheet
f.write(old_heading + g.read())
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 6: ordinal not in range(128)PetrThu, 12 Mar 2015 13:02:07 +0100https://ask.sagemath.org/question/26167/Why Sage can not plot Chinese labelhttps://ask.sagemath.org/question/9109/why-sage-can-not-plot-chinese-label/ I know that Matplotlib does not support UTF-8 without some change in matplotlibrc,So I edit the matplotlibrc and now when plot with matplotlib in sagenb,it supports UTF-8 now and it can show Chinese now.<br>
But for the sage plot function, when I want to set label with some Chinese characters,it raise exception :<br>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u6211' in position 0: ordinal not in range(128).<br>
After I set the default encoding to UTF-8 in site.py,this exception does not raise again,but the Chinese characters are shown as box like [].
Below is my code:
p1=plot(sin,(-2*pi,2*pi),thickness=2.0,rgbcolor=(0.5,1,0),legend_label='sin(x)')
p2=plot(cos,(-2*pi,2*pi),thickness=3.0,color='purple',alpha=0.5,legend_label='cos(x)')
plt=p1+p2
x = u'\xd6\xd0\xb9\xfa\xc8\xcb'
plt.axes_labels((x,'y'))
show(plt)
Is there any solution?
I am sorry, it seems that this web site does not support Chinese, so I change the x shahuwangMon, 25 Jun 2012 04:53:57 +0200https://ask.sagemath.org/question/9109/