Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unicode text when using savefig() in matplotlib

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.

like image 403
Wei Avatar asked Feb 16 '26 03:02

Wei


1 Answers

finally solved it by using:

fontpath = '/Library/Fonts/华文细黑.ttf'
properties = font_manager.FontProperties(fname=fontpath)
matplotlib.rcParams['font.family'] = properties.get_name()
like image 129
Wei Avatar answered Feb 18 '26 17:02

Wei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!