3

I am new to matplotlib and trying to plot a 2D/3D chart with non-ASCII texts in it, I am able to use show() to plot on screen:

plt.text(X, Y, str(text[i].decode('utf-8')) )
plt.show()

However when I try to save the figure to a file with savefig(), all texts become little squares []

I am sure it is a font related problem but I can't see to figure out how to fix this.

I am on a Mac OSX 10.10.5, if that matters.

Pynchia
  • 10,996
  • 5
  • 34
  • 43
Wei
  • 341
  • 2
  • 14
  • Probably a duplicate: http://stackoverflow.com/questions/10960463/non-ascii-characters-in-matplotlib – Bernhard Sep 23 '15 at 10:21
  • Thanks, I googled that question but I try the suggested fonts without luck. – Wei Sep 23 '15 at 11:22
  • Yes, it depends very much on your unicode characters and the character map of the font. E.g. Arial won't have any glyphs for Chinese characters. – Bernhard Sep 23 '15 at 11:32

1 Answers1

1

finally solved it by using:

fontpath = '/Library/Fonts/华文细黑.ttf'
properties = font_manager.FontProperties(fname=fontpath)
matplotlib.rcParams['font.family'] = properties.get_name()
Andrey Portnoy
  • 1,430
  • 15
  • 24
Wei
  • 341
  • 2
  • 14