3

I would like to plot Apple emoji in matplotlib, so I wrote this code. However, I get an error that could not set the font size when setting fonts. If I do not set the font it will be plotted like the next image. image of plotting emoji

My system is Mac OS X Sierra 10.12.6, my matplotlib version is 2.0.2.

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

prop = FontProperties(fname='/System/Library/Fonts/Apple Color Emoji.ttc')
plt.rcParams['font.family'] = prop.get_name()

plt.annotate("", (0.5, 0.3), size=30)
plt.annotate("", (0.5, 0.8), size=30)
plt.savefig("emoji_test.png")

File "./emoji_test.py", line 5, in

plt.rcParams['font.family'] = prop.get_name()

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/font_manager.py", line 750, in get_name

return get_font(findfont(self)).family_name

RuntimeError: In FT2Font: Could not set the fontsize

tommy19970714
  • 87
  • 1
  • 7
  • I think matplotlib has rather limited support of ttc fonts. No matter what, it will sure not be able to show color in fonts. – ImportanceOfBeingErnest Feb 13 '18 at 11:49
  • See [here](http://catherineh.github.io/programming/2017/10/24/emoji-data-markers-in-matplotlib) for how to use normal images instead – ImportanceOfBeingErnest Feb 13 '18 at 11:52
  • Same question as here: https://stackoverflow.com/questions/47102873/how-to-plot-high-quality-emoji-in-matplotlib – ImportanceOfBeingErnest Feb 13 '18 at 11:54
  • I tried ttf fonts, however the result was same. And I used images instead of emoji (same method that you suggested), but I'd like to use emoji fonts because the picture quality is bad. – tommy19970714 Feb 13 '18 at 12:06
  • I found the issue from stackoverflow question that you suggested. [Emoji missing when use plt.savefig](https://github.com/matplotlib/matplotlib/issues/4492/) Emoji can be displayed in this issue, but can not be reproduced in own environment. – tommy19970714 Feb 13 '18 at 12:12
  • Ok, I'd suggest you edit the question to use the code you got from that quesiton and the issue you have using it. Still I think this is not using `ttc` font, but `ttf`. – ImportanceOfBeingErnest Feb 13 '18 at 12:17
  • Thank you very much for researching out variously! – tommy19970714 Feb 13 '18 at 12:20

1 Answers1

1

Use prop.get_family() rather than prop.get_name() since you're setting the font family not the font name

the code becomes.

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

prop = FontProperties(fname='/System/Library/Fonts/Apple Color Emoji.ttc')
plt.rcParams['font.family'] = prop.get_family()

plt.annotate("", (0.5, 0.3), size=30)
plt.annotate("", (0.5, 0.8), size=30)
plt.savefig("emoji_test.png")

The resulting figure is below enter image description here

PS. The error message is due to a known issue. see RuntimeError In FT2Font with NISC18030.ttf

Community
  • 1
  • 1
sgDysregulation
  • 4,309
  • 2
  • 23
  • 31