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

Problems with text that is just a LaTeX minus sign #4147

Closed
jfeist opened this issue Feb 23, 2015 · 9 comments · Fixed by #4148
Closed

Problems with text that is just a LaTeX minus sign #4147

jfeist opened this issue Feb 23, 2015 · 9 comments · Fixed by #4148
Assignees

Comments

@jfeist
Copy link

jfeist commented Feb 23, 2015

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.

@jfeist jfeist changed the title Problems with LaTeX minus sign in legend Problems with text that is just a LaTeX minus sign Feb 23, 2015
@mdboom mdboom self-assigned this Feb 23, 2015
@jfeist
Copy link
Author

jfeist commented Feb 23, 2015

I was looking through the open issues and realized that it could be related to #3374, and indeed it also shows up with just a text call, so an even more minimal example is

import matplotlib.pyplot as plt
plt.text(0.5,0.5,'$-$')
plt.savefig('test.png',dpi=150)

The only difference seems to be that the error now shows up at different dpi values (maybe because of different text sizes?).

I have updated the title to reflect this.

@mdboom
Copy link
Member

mdboom commented Feb 23, 2015

Hmm... Strangely, I can't reproduce on 1.4.2 or 1.4.3 on Mac or Linux. Do you have anything special about your fonts happening, or anything in matplotlibrc? What version of Freetype?

@jenshnielsen
Copy link
Member

I can reproduce it with 1.4.3 (python 2.7 on OSX 101.10 freetype 2.5.5) but not on master.

@jfeist
Copy link
Author

jfeist commented Feb 23, 2015

@mdboom Hmm, surprising - I don't even have a .matplotlibrc, so nothing special there. I haven't done anything special with the fonts either (at least as far as I can remember, but they're fairly new systems, so I think I would). On OS X, I have freetype 2.5.5 from macports, on Red Hat (actually CentOS 6.6 I just realized), I have freetype 2.4.10 from anaconda. Any idea what else I could/should check?

I'm happy @jenshnielsen could reproduce it at least.

@mdboom
Copy link
Member

mdboom commented Feb 23, 2015

Yeah -- just moved to freetype 2.5.5 on both systems and still no breakage for me.

I wouldn't be surprised if this is fixed already on master -- this does go through code paths that were pretty heavily rewritten. Could you try testing git master to see if that resolves your issue?

It would be nice to make a bugfix for 1.4.x if it's easy, but we don't currently have plans for another 1.4.x release at the moment.

@jfeist
Copy link
Author

jfeist commented Feb 23, 2015

I just tested with matplotlib master (installed in a clean virtualenv), and indeed I do not get the exceptions anymore (i.e., it doesn't crash with any setting of dpi). But the minus sign still disappears for dpi>93 or so. So now it simply fails silently in all cases, while before it sometimes at least gave an error...

@mdboom
Copy link
Member

mdboom commented Feb 23, 2015

Ah -- well there's something I can reproduce. Now onto a solution...

@jenshnielsen
Copy link
Member

I can reproduce that too. However, the error message only shows up sporadically and only when calling save together with the inline backend.

@jfeist
Copy link
Author

jfeist commented Feb 25, 2015

Thanks for the quick fix! I can confirm it also works for me. However, I noticed that #3374 is not fixed by this. I'll provide more details there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants