Description
I just discovered that a legend with label '$-$'
, i.e., a single math-mode minus sign, has a number of problems in png output (which also is problematic for ipython notebook inline plots, but the error is independent of that). The exact problem depends on the dpi that the figure is saved with. Here's a minimal example to reproduce it:
import matplotlib.pyplot as plt
plt.plot([0,1],[1,0])
plt.legend(['$-$'])
plt.savefig('test.png',dpi=120)
For some settings of dpi, I get an error message with a traceback ending in
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 187, in draw_text
return self.draw_mathtext(gc, x, y, s, prop, angle)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 178, in draw_mathtext
self._renderer.draw_text_image(font_image, x, y + 1, angle, gc)
ValueError: First argument to draw_text_image must be a FT2Font.Image object or a Nx2 uint8 numpy array.
while for other settings of dpi, the PNG is generated but the minus sign does not show up (i.e., the legend is empty). Both only happens if the minus sign is the only character in the legend - although it also happens with two minus signs $--$
, and even a dash and a minus sign -$-$
, but not with only a dash -
, or if I add any other character (as far as I have tested).
The exact settings of dpi where I get the error and where the minus sign disappears from the output are apparently system-dependent, but it always disappears for high enough dpi (>93 on one system), and the errors typically show up in the range 120<dpi<140 - for higher dpi, there is again no error, but the symbol is not shown either.
I have tested this on two different systems (OS X with matplotlib 1.4.2 from MacPorts and Red Hat with matplotlib 1.4.3 from anaconda), and with both python 2.7 and python 3.4 on the OS X system, and it shows up on all of them.