Hi all,
I have encountered an error trying to SVG export a figure containing a unicode symbol.
Code:
import pyqtgraph as pg
import pyqtgraph.exporters as pgex
plt1=pg.plot([0,1],[0,1])
plt1.plotItem.setTitle('psi')
exp=pgex.SVGExporter(plt1.plotItem)
exp.export('no_unicode.svg') # works fine
plt2=pg.plot([0,1],[0,1])
plt2.plotItem.setTitle(u'\u03c8(x)') # plot displays with nice greek psi in title
exp=pgex.SVGExporter(plt2.plotItem)
exp.export('unicode.svg') # gives error
Error:
Traceback (most recent call last):
File "/media/dane/hd/code/python/bugs/demo_pyqtgraph_exporter_problem_06012016.py", line 12, in <module>
exp.export('unicode.svg') # gives error
File "/home/dane/hd/code/python/packages/pyqtgraph/exporters/SVGExporter.py", line 52, in export
xml = generateSvg(self.item)
File "/home/dane/hd/code/python/packages/pyqtgraph/exporters/SVGExporter.py", line 75, in generateSvg
node, defs = _generateItemSvg(item)
File "/home/dane/hd/code/python/packages/pyqtgraph/exporters/SVGExporter.py", line 270, in _generateItemSvg
csvg = _generateItemSvg(ch, nodes, root)
File "/home/dane/hd/code/python/packages/pyqtgraph/exporters/SVGExporter.py", line 270, in _generateItemSvg
csvg = _generateItemSvg(ch, nodes, root)
File "/home/dane/hd/code/python/packages/pyqtgraph/exporters/SVGExporter.py", line 193, in _generateItemSvg
doc = xml.parseString(xmlStr)
File "/home/dane/anaconda/lib/python2.7/xml/dom/minidom.py", line 1928, in parseString
return expatbuilder.parseString(string)
File "/home/dane/anaconda/lib/python2.7/xml/dom/expatbuilder.py", line 940, in parseString
return builder.parseString(string)
File "/home/dane/anaconda/lib/python2.7/xml/dom/expatbuilder.py", line 223, in parseString
parser.Parse(string, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u03c8' in position 1253: ordinal not in range(128)
I'm not sure if this is a bug or a limitation. Happy to investigate solutions myself if given some pointers.
Dane
xmlStr = bytes(arr).decode('utf-8')
to
xmlStr = bytes(arr)#.decode('utf-8')
appears to solve the problem. I can now export plots with Greek letters, subscript and superscript.