Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: backend_pdf: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 #3076

Merged
merged 1 commit into from May 19, 2014

Conversation

mdboom
Copy link
Member

@mdboom mdboom commented May 19, 2014

I tried this simple example on python 2.7.2:
(matplotlib 1.4.x)

import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import rc
#rc('font',**{'family':'serif'})
rc('text', usetex=True)
rc('text.latex',unicode=True)
rc('text.latex',preamble='\usepackage[utf8]{inputenc}')
rc('text.latex',preamble='\usepackage[russian]{babel}')

f = plt.figure()
ax = f.add_subplot(111)
ax.set_xlabel(u'время, с')
plt.show()
f.savefig('stepped_exp.pdf')

But I got:

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-2-74334d19869a> in <module>()
     14 ax.set_xlabel(u'время, с')
     15 plt.show()
---> 16 f.savefig('stepped_exp.pdf')
     17 

/Users/dmitry/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.6-x86_64.egg/matplotlib/figure.pyc in savefig(self, *args, **kwargs)
   1468             self.set_frameon(frameon)
   1469 
-> 1470         self.canvas.print_figure(*args, **kwargs)
   1471 
   1472         if frameon:

/Users/dmitry/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.6-x86_64.egg/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2190                 orientation=orientation,
   2191                 bbox_inches_restore=_bbox_inches_restore,
-> 2192                 **kwargs)
   2193         finally:
   2194             if bbox_inches and restore_bbox:

/Users/dmitry/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.6-x86_64.egg/matplotlib/backends/backend_pdf.pyc in print_pdf(self, filename, **kwargs)
   2468                 RendererPdf(file, image_dpi),
   2469                 bbox_inches_restore=_bbox_inches_restore)
-> 2470             self.figure.draw(renderer)
   2471             renderer.finalize()
   2472         finally:

/Users/dmitry/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.6-x86_64.egg/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     57     def draw_wrapper(artist, renderer, *args, **kwargs):
     58         before(artist, renderer)
---> 59         draw(artist, renderer, *args, **kwargs)
     60         after(artist, renderer)
     61 

/Users/dmitry/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.6-x86_64.egg/matplotlib/figure.pyc in draw(self, renderer)
   1077         dsu.sort(key=itemgetter(0))
   1078         for zorder, a, func, args in dsu:
-> 1079             func(*args)
   1080 
   1081         renderer.close_group('figure')

/Users/dmitry/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.6-x86_64.egg/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     57     def draw_wrapper(artist, renderer, *args, **kwargs):
     58         before(artist, renderer)
---> 59         draw(artist, renderer, *args, **kwargs)
     60         after(artist, renderer)
     61 

/Users/dmitry/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.6-x86_64.egg/matplotlib/axes/_base.pyc in draw(self, renderer, inframe)
   2073 
   2074         for zorder, a in dsu:
-> 2075             a.draw(renderer)
   2076 
   2077         renderer.close_group('axes')

/Users/dmitry/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.6-x86_64.egg/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     57     def draw_wrapper(artist, renderer, *args, **kwargs):
     58         before(artist, renderer)
---> 59         draw(artist, renderer, *args, **kwargs)
     60         after(artist, renderer)
     61 

/Users/dmitry/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.6-x86_64.egg/matplotlib/axis.pyc in draw(self, renderer, *args, **kwargs)
   1115         self._update_label_position(ticklabelBoxes, ticklabelBoxes2)
   1116 
-> 1117         self.label.draw(renderer)
   1118 
   1119         self._update_offset_text_position(ticklabelBoxes, ticklabelBoxes2)

/Users/dmitry/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.6-x86_64.egg/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     57     def draw_wrapper(artist, renderer, *args, **kwargs):
     58         before(artist, renderer)
---> 59         draw(artist, renderer, *args, **kwargs)
     60         after(artist, renderer)
     61 

/Users/dmitry/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.6-x86_64.egg/matplotlib/text.pyc in draw(self, renderer)
    578             if rcParams['text.usetex']:
    579                 renderer.draw_tex(gc, x, y, clean_line,
--> 580                                   self._fontproperties, angle, mtext=self)
    581             else:
    582                 renderer.draw_text(gc, x, y, clean_line,

/Users/dmitry/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.6-x86_64.egg/matplotlib/backends/backend_pdf.pyc in draw_tex(self, gc, x, y, s, prop, angle, ismath, mtext)
   1823             # We need to convert the glyph numbers to bytes, and the easiest
   1824             # way to do this on both Python 2 and 3 is .encode('latin-1')
-> 1825             seq += [['text', x1, y1, [chr(glyph).encode('latin-1')], x1+width]]
   1826 
   1827         # Find consecutive text strings with constant y coordinate and

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)

It seems that this fix mdboom@38d846b

brakes the compatibility with cyrilic symbols.

The previous edition of backend_pdf.py with:

seq += [['text', x1, y1, [chr(glyph)], x1+width]]

Works just fine to me.

Or I'm doing something wrong?

Thanx!

@dmitryikh dmitryikh changed the title Bug: backend_pdf: In [2]: import pandas as pd import matplotlib as mpl import matplotlib.pyplot as plt from matplotlib import rc #rc('font',**{'family':'serif'}) rc('text', usetex=True) rc('text.latex',unicode=True) rc('text.latex',preamble='\usepackage[utf8]{inputenc}') rc('text.latex',preamble='\usepackage[russian]{babel}') f = plt.figure() ax = f.add_subplot(111) ax.set_xlabel(u'время, с') plt.show() f.savefig('stepped_exp.pdf') ...: --------------------------------------------------------------------------- UnicodeDecodeError Traceback (most recent call last) <ipython-input-2-74334d19869a> in <module>() 14 ax.set_xlabel(u'время, с') 15 plt.show() ---> 16 f.savefig('stepped_exp.pdf') 17 /Users/dmitry/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.6-x86_64.egg/matplotlib/figure.pyc in savefig(self, *args, **kwargs) 1468 self.set_frameon(frameon) 1469 -> 1470 self.canvas.prin Bug: backend_pdf: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 May 19, 2014
@tacaswell
Copy link
Member

What version of mpl are you using?

On current master I get issues coming out of latex (I assume that my system latex isn't set up for cyrilic/I don't have the Russian babel package). Not sure if this is before or after the line where you get the error.

If I turn usetex=False it works (get_xlabel() return the correct string), but I don't have the right fonts so I don't see the text in the figure.

@tacaswell tacaswell added this to the v1.4.0 milestone May 19, 2014
@tacaswell tacaswell added the text label May 19, 2014
@dmitryikh
Copy link
Author

matplotlib 1.4.x (master branch).

I also had an error from the latex on my system if I comment the line:

#rc('text.latex',preamble='\usepackage[russian]{babel}')

But with this line turned on I have an error on mpl side.. I'm out of understanding how the latex <-> matplotlib interconnection works, but for me this issue makes some trouble:) And the solution for me - roll back one line of code in backend_pdf.py

@mdboom
Copy link
Member

mdboom commented May 19, 2014

I think the attached should fix this. @dmitryikh: Can you confirm?

Unfortunately, I'm not sure how to write a test for this, since we don't currently assume a latex install for our tests.

@tacaswell
Copy link
Member

We could just make travis install latex and have a knownfail like we do with the animation tests in cases where it is not installed.

@mdboom
Copy link
Member

mdboom commented May 19, 2014

That would be ideal. I wonder if installing something as big as LaTeX (with the necessary international packages to test something like this) would use up our Travis quota too much. We could do it in only one of the four configurations, I suppose, as a way to ameliorate that.

@efiring
Copy link
Member

efiring commented May 19, 2014

Travis is already taking a long time to run, and generating reams of output--I would really hesitate before pulling in LaTeX each time. A standard linux LaTeX install, at least on ubuntu, pulls in a huge mound of packages.
Ideally, there should be a way to save virtual testing machines that already have everything except mpl installed--is there?

@mdboom
Copy link
Member

mdboom commented May 19, 2014

I think that's been "coming to Travis" for a long time, but they aren't there yet.

@dmitryikh
Copy link
Author

@mdboom, it fixed this!:)

@efiring
Copy link
Member

efiring commented May 19, 2014

I think we should go with this PR as is, and defer any LaTeX-based Travis tests.

tacaswell added a commit that referenced this pull request May 19, 2014
Bug: backend_pdf: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2
@tacaswell tacaswell merged commit 7f7aa5e into matplotlib:master May 19, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants