[Python] Japanese font setting method for matplot lib 3.2 [Official compliance]

: star: Introduction

--matplotlib (https://matplotlib.org/) is a well-known library for drawing graphs etc. in Python. ――However, if you want to use Japanese for labels in the graph, garbled characters (◻︎◻︎◻︎◻︎◻︎, so it seems to be called tofu) will occur by default. ――The article that came out when I searched in matplotlib Japanese was quite troublesome with the method of power technique (such as the one that directly rewrites the contents of the installed matplotlib), so I will use the method introduced officially. I will write

: question: Don't you use the Japanese localization library?

--There is a kind person who makes a Japanese localization library of matpotlib called japanize-matplotlib (https://github.com/uehara1414/japanize-matplotlib). --Although you can use this, the method font_manager.createFontList used in this library has been deprecated from 3.2.0 of matpotlib.

font_manager.createFontList is deprecated. font_manager.FontManager.addfont is now available to register a font at a given path.

--Therefore, when using matpotlib with 3.2.0 or higher, the following warning is displayed.

MatplotlibDeprecationWarning: 
The createFontList function was deprecated in Matplotlib 3.2 and will be removed two minor releases later. Use FontManager.addfont instead.

--As you can see by reading the source code of japanize-matplotlib, we haven't done anything complicated, so this time we will replace the equivalent implementation with FontManager.addfont.

: pencil: Add Japanese fonts with FontManager.addfont

--Download and unzip the Japanese font (IPAex Gothic this time) --IPAex font: https://ipafont.ipa.go.jp/node193 --Put the downloaded .ttf file in your repository --For example, create a fonts directory and put it like /fonts/ipag.ttf -Just load with fontManager.addfont ―― ~~ I wasted my time thinking that I was a class method ... ~~

from matplotlib import font_manager

font_manager.fontManager.addfont("/fonts/ipag.ttf")
matplotlib.rc('font', family="IPAGothic")

--If you have multiple fonts, do the following:

from matplotlib import font_manager

font_files = font_manager.findSystemFonts(fontpaths=["/fonts"])

for font_file in font_files:
    font_manager.fontManager.addfont(font_file)

matplotlib.rc('font', family="IPAGothic")

: tada: Bonus

--By the way, if the version is less than 3.2, you can handle it as follows. --Almost the same as doing with japanize-matplotlib

from matplotlib import font_manager

font_files = font_manager.findSystemFonts(fontpaths=["/fonts"])
font_list = font_manager.createFontList(font_files) # 3.If it is 2 or more, a warning will be issued.
font_manager.fontManager.ttflist.extend(font_list)
matplotlib.rc('font', family="IPAGothic")

: star: At the end

――I haven't done much, but I'll write it down so that it won't be confused when someone else looks it up.

Recommended Posts

[Python] Japanese font setting method for matplot lib 3.2 [Official compliance]
python Environmentally-friendly Japanese setting
tesseract-OCR for Python [Japanese version]
Easy Japanese font setting with matplotlib