Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

But if I switch to 'sage' on top of the page, it works as expected:

example of the valid output

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 :-( :-(

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

But if I switch to 'sage' on top of the page, it works as expected:

example of the valid output

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 :-( :-(

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.