[matplotlib-devel] Small fix for SVG backend
Hi, I tried to use matplotlib.rcParams['svg.embed_char_paths'] = False in order to have editable text in exported SVG, but there was an encoding issue and the file data could not be written (traceback appended). Therefore I exchanged the XML character escaping from sax in backend_svg.py with python's native XML escape capability (see appended diff), and voilà it works! The conversion of mathtext to SVG is still not so nice because specific fonts (cmmi10, cmr10, cmsy10) are used that encode mathematic symbols with other unrelated characters. The formula display will totally break if these fonts aren't installed, and editing is a pain. It would therefore be great if unicode characters were used so that the correct display is not so much dependent on those specific fonts. Would this introduce other problems and where would be the best point to make that change? Greetings, Dieter Traceback (most recent call last): File "/home/weber/Promotion/svn/report/MainWindow.py", line 218, in onExport figure.savefig(tmp, format=format, transparent=True) File "/usr/local/lib/python2.6/dist-packages/matplotlib/figure.py", line 1160, in savefig self.canvas.print_figure(*args, **kwargs) File "/usr/local/lib/python2.6/dist-packages/matplotlib/backends/backend_wxagg.py", line 100, in print_figure FigureCanvasAgg.print_figure(self, filename, *args, **kwargs) File "/usr/local/lib/python2.6/dist-packages/matplotlib/backend_bases.py", line 1963, in print_figure **kwargs) File "/usr/local/lib/python2.6/dist-packages/matplotlib/backend_bases.py", line 1758, in print_svg return svg.print_svg(*args, **kwargs) File "/usr/local/lib/python2.6/dist-packages/matplotlib/backends/backend_svg.py", line 870, in print_svg return self._print_svg(filename, svgwriter, fh_to_close, **kwargs) File "/usr/local/lib/python2.6/dist-packages/matplotlib/backends/backend_svg.py", line 905, in _print_svg self.figure.draw(renderer) File "/usr/local/lib/python2.6/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper draw(artist, renderer, *args, **kwargs) File "/usr/local/lib/python2.6/dist-packages/matplotlib/figure.py", line 874, in draw func(*args) File "/usr/local/lib/python2.6/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper draw(artist, renderer, *args, **kwargs) File "/usr/local/lib/python2.6/dist-packages/matplotlib/axes.py", line 1954, in draw a.draw(renderer) File "/usr/local/lib/python2.6/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper draw(artist, renderer, *args, **kwargs) File "/usr/local/lib/python2.6/dist-packages/matplotlib/axis.py", line 986, in draw tick.draw(renderer) File "/usr/local/lib/python2.6/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper draw(artist, renderer, *args, **kwargs) File "/usr/local/lib/python2.6/dist-packages/matplotlib/axis.py", line 234, in draw self.label1.draw(renderer) File "/usr/local/lib/python2.6/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper draw(artist, renderer, *args, **kwargs) File "/usr/local/lib/python2.6/dist-packages/matplotlib/text.py", line 591, in draw ismath=ismath) File "/usr/local/lib/python2.6/dist-packages/matplotlib/backends/backend_svg.py", line 676, in draw_text write(svg) File "/usr/lib/python2.6/codecs.py", line 801, in write data, bytesdecoded = self.decode(data, self.errors) File "/usr/lib/python2.6/encodings/utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeEncodeError: 'ascii' codec can't encode character u'\u2212' in position 155: ordinal not in range(128) Index: lib/matplotlib/backends/backend_svg.py === --- lib/matplotlib/backends/backend_svg.py (Revision 8841) +++ lib/matplotlib/backends/backend_svg.py (Arbeitskopie) @@ -23,8 +23,11 @@ from matplotlib.transforms import Affine2D from matplotlib import _png -from xml.sax.saxutils import escape as escape_xml_text +#from xml.sax.saxutils import escape as escape_xml_text +def escape_xml_text(s): +return s.encode('ascii', 'xmlcharrefreplace') + backend_version = __version__ def new_figure_manager(num, *args, **kwargs): -- Lotusphere 2011 Register now for Lotusphere 2011 and learn how to connect the dots, take your collaborative environment to the next level, and enter the era of Social Business. http://p.sf.net/sfu/lotusphere-d2d___ Matplotlib-devel mailing list Matplotlib-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
Re: [matplotlib-devel] Small fix for SVG backend
Unfortunately, this isn't a complete solution. The purpose of the "escape_xml_chars" function is to escape characters that have special meaning in XML, e.g. "&" -> "&", "<" -> "<" etc. The "xmlcharrefreplace" option on encode() does not do that. I am surprised that you got this traceback, as the SVG is output using a UTF-8 EncodedFile object, which should be able to handle any unicode characters you throw at it. Can you provide a short, standalone example that reproduces the error, and some information about your platform, so I can examine further? As for your suggestion to encode mathematical characters as unicode, unfortunately that would only partially. In order to layout the mathematical expressions, we must have the same font available at generation and rendering time to know the size of the characters and therefore how to place them next to each other correctly. That said, if you use the STIX fonts (set "mathtext.fontset" to "STIX"), those do use a Unicode-based encoding rather than the arbitrary one used by the Bakoma Computer Modern fonts. Of course, generating a math expression using the STIX fonts and rendering it using a different Unicode math font is not guaranteed to work well -- you are almost certain to get overlapping or misplaced glyphs. Mike -- Lotusphere 2011 Register now for Lotusphere 2011 and learn how to connect the dots, take your collaborative environment to the next level, and enter the era of Social Business. http://p.sf.net/sfu/lotusphere-d2d ___ Matplotlib-devel mailing list Matplotlib-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
Re: [matplotlib-devel] Small fix for SVG backend
Hi Mike, the error only occurs if the output file is specified as a file(-like) object instead of a file name. This is necessary for me in order to add a matplotlib-generated file to a tar archive via a safe temporary file. Greetings, Dieter #!/usr/bin/env python # -*- coding: utf-8 -*- import matplotlib import matplotlib.pyplot as pp import tempfile import cStringIO as StringIO matplotlib.rcParams['svg.embed_char_paths'] = False pp.plot([1, 2], [1, 2], label=u"äöü") pp.legend() # works pp.savefig('test.svg') # breaks with open('test2.svg', 'wb') as ofile: pp.savefig(ofile, format='svg') # breaks ostr = StringIO.StringIO() pp.savefig(ostr, format='svg') -- Lotusphere 2011 Register now for Lotusphere 2011 and learn how to connect the dots, take your collaborative environment to the next level, and enter the era of Social Business. http://p.sf.net/sfu/lotusphere-d2d___ Matplotlib-devel mailing list Matplotlib-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
[matplotlib-devel] Displaying offsets in 3d plots
I have been working on a new feature for mplot3d. It is a bit of a hack but with this patch the offset information in a tick formatter can now be displayed. The offset text is located so that the offset texts for two jointed axis are not co-located, and is placed at the same distance away from the axis as the axis label. The text is aligned so that it should always be "tucked" underneath the axis as one rotates the plot. There are a few edge cases as one rotates where an offset text will "stick out", but they should be very infrequent. I have attached a patch to enable this feature and also an example script that showcases it. This is how it should look: http://dl.dropbox.com/u/7325604/mplot3d_offsettest.png Let me know what you think, and if all is good, I will add it to the development branch soon. Ben Root mplot3d_offsets.patch Description: Binary data mplot3d_offsetdemo.py Description: Binary data -- Lotusphere 2011 Register now for Lotusphere 2011 and learn how to connect the dots, take your collaborative environment to the next level, and enter the era of Social Business. http://p.sf.net/sfu/lotusphere-d2d___ Matplotlib-devel mailing list Matplotlib-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
Re: [matplotlib-devel] Small fix for SVG backend
What platform are you on? It "works for me" on Fedora 14, RHEL 5 and Cygwin. Mike On 12/16/2010 10:59 AM, Dieter Weber wrote: > #!/usr/bin/env python > # -*- coding: utf-8 -*- > import matplotlib > import matplotlib.pyplot as pp > import tempfile > import cStringIO as StringIO > > matplotlib.rcParams['svg.embed_char_paths'] = False > > pp.plot([1, 2], [1, 2], label=u"äöü") > pp.legend() > > # works > pp.savefig('test.svg') > > # breaks > with open('test2.svg', 'wb') as ofile: > pp.savefig(ofile, format='svg') > > # breaks > ostr = StringIO.StringIO() > pp.savefig(ostr, format='svg') -- Lotusphere 2011 Register now for Lotusphere 2011 and learn how to connect the dots, take your collaborative environment to the next level, and enter the era of Social Business. http://p.sf.net/sfu/lotusphere-d2d ___ Matplotlib-devel mailing list Matplotlib-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-devel