5

I've been playing with the xkcd style feature in matplotlib. I have Matplotlib 1.4, and the humor sans font is installed correctly, or works in msword at least.

When I run the following example code, I get "?" instead of "-" on labels any time there should be a negative number. Any idea what causes this?

with plt.xkcd():
  plt.figure()
  plt.plot(np.sin(np.linspace(0, 10)))
  plt.title('Whoo Hoo!!!')

enter image description here

Edit: At Bernie's suggestion (thanks Bernie) I tried some different backends. Tried Qt, Tkinter, and inline, and none seem to work. I'm using Ipython 2.3 btw.

Also, reputation is now high enough to post an image. As you can see everything is as expected except those pesky question marks.

Nathan Kiner
  • 373
  • 3
  • 9
  • 1
    Negative values display appropriately in my test plot. Which backend are you using? You can find that by doing: `import matplotlib; matplotlib.get_backend()` – mechanical_meat Oct 10 '14 at 17:17
  • Bernie, I was using QT4Agg . I switched to the IPython inline backend 'module://IPython.kernel.zmq.pylab.backend_inline', and other than the fact that it displays inline I get the same results. Tried Tkinter (TkAgg) and that doesn't work either. Weird. – Nathan Kiner Oct 10 '14 at 18:24
  • Hm. I'm out of ideas for now... – mechanical_meat Oct 10 '14 at 18:28
  • 1
    I also can't reproduce this, so it is probably something to do with your font installation. Maybe check the actual humor sans minus character in Word to see if the symbol works? – Ajean Oct 10 '14 at 18:36
  • Appears to work correctly in word. Is there a newer version of humor sans than 1.0? Cause that's what I've got. – Nathan Kiner Oct 10 '14 at 18:58
  • 1
    Note that the minus sign on your keyboard doesn't have to be the minus sign used in the XKCD plots. There's a [difference between a hyphen, minus sign, n-dash and m-dash](http://www.punctuationmatters.com/the-difference-between-a-dash-and-a-minus-sign/). So it may indeed still be a font problem, unless you tried all the various hyphen/minus/dashes in Word. –  Oct 10 '14 at 19:03
  • 1
    It's using a "minus sign" (i.e. U+2212) for me in Linux, not the ASCII "hyphen-minus". `plt.ion(); with plt.xkcd(): p, = plt.plot(np.sin(np.linspace(0, 10)));` `c = p.axes.yaxis.get_ticklabels()[0].get_text()[0];` `ord(c) == 0x2212;` `unicodedata.name(c) == 'MINUS SIGN'`. On Windows, "?" is typically substituted for an unmapped character when encoding to a codepage such as 1252. – Eryk Sun Oct 11 '14 at 01:55
  • Well, you guys are on to something, it obviously has SOMETHING to do with the font, because I uninstalled humor sans, and now negatives display correctly. Of course I don't get the xkcd style font anymore, it appears to be defaulting to comic sans. Still though, the docstring specifically mentions that you should install humor sans for best results, so I dont understand why that didn't work. – Nathan Kiner Oct 11 '14 at 02:22
  • Check this [post](http://stackoverflow.com/questions/19663986/getting-xkcd-plots-using-matplotlib). It seems that you need to remove the maptlotlib font cache. – nicoguaro Oct 20 '14 at 20:02

1 Answers1

2

eryksun comment is right. Thus, you must add the MINUS SIGN (U+2212) character to the xkcd font (hunour sans). To do so, use a font editor (I used FontForge):

  1. From the FontForge menu, open the Humour Sans ttf file.
  2. Copy paste the HYPHEN-MINUS (U+002d) glyph to the MINUS SIGN (U+2212) character box (they are ordered in increasing Unicode number).
  3. Go to File -> Generate Font. Choose True Type (.ttf) and save it in a different folder than your Fonts folder.
  4. Install the newly saved Font. (on Windows, double click to open a preview window, then click install)

I had the same problem as you and that solved it.

Ludo
  • 36
  • 5
  • 1
    Thanks Ludo and Eryksun, that did the trick. BTW, the version of FontForge you linked to crashed repeatedly on my win7 machine. I got it done with the windows builds located here: http://fontforgebuilds.sourceforge.net/ – Nathan Kiner Nov 29 '14 at 17:08