Created
October 5, 2016 08:04
-
-
Save Tarrasch/b45f2a393f9ca13c125c74ab821e685f to your computer and use it in GitHub Desktop.
Fix font for Vietnamese plots in matplotlib python lib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I Used this with a IPython notebook using pandas, but I suppose that part isn't relevant | |
# | |
# Big thanks to guy who wrote this: http://www.himpactwxlab.com/home/how-to-wiki/change-matplotlib-fonts | |
# | |
# I had the problem that vietnamese characters showed as empty boxes. Although tones displayed properly. | |
# Here's the code to fix it: | |
import matplotlib | |
import matplotlib.font_manager as font_manager | |
sorted(font_manager.findSystemFonts()) # Print this to see available fonts | |
fontpath = '/usr/share/fonts/truetype/freefont/FreeSans.ttf' | |
# Naturally I picked the font with the most "GNU-ish" name, assuming it has good i18n | |
# (assumption turned out to be correct :D) | |
prop = font_manager.FontProperties(fname=fontpath) | |
matplotlib.rcParams['font.family'] = prop.get_name() | |
# Done, now you can use it, like: | |
my_pandas_dataframe.plot(kind='bar') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment