[PYTHON] [Seaborn] Display Japanese (change font)

Seaborn? Seaborn is one of Python's external modules created for the purpose of visualizing statistical data. On the official page

Seaborn is a Python visualization library based on matplotlib. It provides a high-level interface for drawing attractive statistical graphics. (Seaborn is a Python visualization library based on matplotlib. It provides a high level interface for drawing attractive statistical graphs.)

It is explained. In short, it's a library that makes matplotlib cool and easy to use. However, Seaborn cannot display Japanese well by default due to font problems 1 </ sub>.

import seaborn as sns
sns.plt.plot([0,1], [0,1]); sns.plt.title('tofu -tofu')

tofu1.png

Therefore, I would like to rewrite the font so that it can display Japanese.

Symptomatic treatment

As a temporary measure, there is a method of "specifying the font to be used on the spot":

sns.set(font='Yu Gothic')
sns.plt.plot([0,1], [0,1]);  sns.plt.title('tofu -tofu')

tofu2.png However, with this method, you have to set it every time you import seaborn, and the font is returned to the default value at some timing 2 < You need to reconfigure / sub>.

Cause therapy?

In the first place, you should be able to solve the problem by playing with the default value of seaborn. I will look at them in order.

Add fonts to matplotlib

seaborn runs on matplotlib, so you need to add fonts to matplotlib to use your favorite fonts.

Font download

Anything is fine, but this time I will use IPAex font. After downloading and unzipping, you should see a file called ʻipaexg.ttf`.

Find the location of the matplotlib config file and add the font

import matplotlib as mpl
print(mpl.matplotlib_fname())

Try to run. You will see the path to the configuration file that your matplotlib is currently referencing. The default is python_install_folder \\ lib \\ site-packages \\ matplotlib \\ mpl-data \\ matplotlibrc. Copy the file ʻipaexg.ttfthat you downloaded earlier to the folder wherematplotlibrc exists, that is, the ttffolder of thefonts folder under mpl-data`. Matplotlib can now use IPAex fonts 3 </ sub>.

Rewrite seaborn config file

Open rcmod.py in the seaborn folder in site-packages with an editor. Comment out the def set (context =" notebook ", ... on lines 66-67 and add the following line:

def set(context="notebook", style="darkgrid", palette="deep",
        font="IPAexGothic", font_scale=1, rc=None):

In addition, comment out the " font.family ": [" sans-serif "] around lines 199-201 and add the following line:

"font.family": ["IPAexGothic"],

Now you are ready 4 </ sub>. Let's launch Python again and check it.

import seaborn as sns
sns.plt.plot([0,1], [0,1]);  sns.plt.title('tofu -tofu')

tofu3.png Good!

By the way

import matplotlib as mpl
font_paths = mpl.font_manager.findSystemFonts()
font_objects = mpl.font_manager.createFontList(font_paths)
font_names = [f.name for f in font_objects]
print(font_names)

When you run, matplotlib will spit out a list of currently available font names (Default fonts in Seaborn statistical data visualization in iPython).


1. I can't display Japanese on Windows, but I don't know what's happening on Mac and Linux. If you can't display Japanese, you should be able to solve it in the same way. 2. sns.set () will restore the default 3. It seems that this folder may be overwritten when matplotlib is updated. 4. This folder is likely to be overwritten when Seaborn is updated.

Recommended Posts

[Seaborn] Display Japanese (change font)
Japanese display of matplotlib, seaborn
Display Japanese in JSON file
Easy Japanese font setting with matplotlib
Change font size in Serene Linux
(Mac) How to display Japanese with Matplotlib and Seaborn At MacOS Sierra