#!/usr/bin/env python # coding: utf-8 # # `matplotlib` font listing # `matplotlib` has [font_manager](http://matplotlib.org/api/font_manager_api.html) to manage fonts across platforms. This notebook checks current font settings and available fonts on the system. # In[1]: import matplotlib as mpl import matplotlib.font_manager as fm import pandas as pd # Show current font settings via *rcParams*. # In[2]: for k in filter(lambda k: k.startswith('font'), sorted(mpl.rcParams)): print('-', k, ':', mpl.rcParams.get(k)) # Show available fonts in this system. *findSystemFonts()* returns list of paths of font files. # In[3]: fonts = fm.findSystemFonts() len(fonts) # `FontProperties` provides access methods from given font file. # For pretty printing, pack the values on dataframe. # In[4]: l = [] for f in fonts: font = fm.FontProperties(fname=f) l.append((f, font.get_name(), font.get_family())) df = pd.DataFrame(l, columns=['path', 'name', 'family']) df # Filter *truetype* fonts. # In[5]: df[df['path'].apply(lambda s: 'truetype' in s)] # Filter IPA fonts after putting [IPA Font](http://ipafont.ipa.go.jp/) files under `/usr/share/fonts/truetype`. # # ``` # $ docker exec notebook cp -r /dataset/IPAfont00303 /usr/share/fonts/truetype # ``` # In[6]: df[df['path'].apply(lambda s: 'IPA' in s)] # ## Sample graph using `matplotlib.pyplot` # In[7]: get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib.pyplot as plt # Default font family does not handle Japanese font. # In[8]: plt.plot([1,2,3,4]) plt.ylabel('数値') plt.show() # Changing *font.family* setting enables Japanese font if available in the system. # Be sure to set *fontdict* when calling *ylabel()* method. # In[9]: plt.plot([1,2,3,4]) plt.ylabel('数値', fontdict={'family': 'IPAPGothic'}) plt.show() # ## Sample graph using `seaborn` # In[10]: import seaborn as sns # In[11]: data = pd.DataFrame([1, 2, 3, 4], columns=['数値']) sns.jointplot(x='数値', y='数値', data=data, xlim=(0, 5), ylim=(0, 5)) # Enable Japanese font setting to call *set()* method, which affects various configuration globally. # In[12]: sns.set(font=['IPAPGothic', 'IPAGothic']) sns.jointplot(x='数値', y='数値', data=data, xlim=(0, 5), ylim=(0, 5)) # Here is the list of current axes styles. # In[13]: sns.axes_style() # Once enable Seaborn, matplotlib default settings was affected, not only fonts but also style including background color. # In[14]: plt.plot([1,2,3,4]) plt.ylabel('数値') plt.show() # In other words, super easy confugration is calling `set()` via Seaborn. # In[ ]: