UnicodeDecodeError in matplotlib if 'python' set instead of 'sage' in Notebook [closed]

asked 2017-04-21 21:24:54 +0200

Eugene gravatar image

updated 2017-04-22 09:34:22 +0200

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.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Eugene
close date 2017-04-22 09:34:53.457045