Skip to content

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

Closed

Description

@jfeist

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.

Activity

  1. changed the title [-]Problems with LaTeX minus sign in legend[/-] [+]Problems with text that is just a LaTeX minus sign[/+] on Feb 23, 2015
  2. self-assigned this
    on Feb 23, 2015
  3. jfeist commented on Feb 23, 2015

    @jfeist
    Author

    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.

  4. mdboom commented on Feb 23, 2015

    @mdboom
    Member

    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?

  5. jenshnielsen commented on Feb 23, 2015

    @jenshnielsen
    Member

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

  6. jfeist commented on Feb 23, 2015

    @jfeist
    Author

    @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.

  7. mdboom commented on Feb 23, 2015

    @mdboom
    Member

    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.

  8. jfeist commented on Feb 23, 2015

    @jfeist
    Author

    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...

  9. mdboom commented on Feb 23, 2015

    @mdboom
    Member

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

  10. jenshnielsen commented on Feb 23, 2015

    @jenshnielsen
    Member

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

  11. added a commit that references this issue on Feb 23, 2015
  12. jfeist commented on Feb 25, 2015

    @jfeist
    Author

    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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions