Skip to content

Instantly share code, notes, and snippets.

@Tarrasch
Created October 5, 2016 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tarrasch/b45f2a393f9ca13c125c74ab821e685f to your computer and use it in GitHub Desktop.
Save Tarrasch/b45f2a393f9ca13c125c74ab821e685f to your computer and use it in GitHub Desktop.
Fix font for Vietnamese plots in matplotlib python lib
# 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