[Python] Let's make matplotlib compatible with Japanese

Overview

Since matplotlib does not support Japanese with standard fonts ...

The characters will be garbled like this. this Let's make it like this.

environment

# OS version
ProductName:	Mac OS X
ProductVersion:	10.15.6
BuildVersion:	19G2021

# Python version
Python 3.8.5
# matplotlib version
matplotlib==3.3.0

procedure

1. Font download

First, download the font as you like. This time, we will use IPA Font.

2. Move the downloaded ttf file to the font folder

Move the ttf file to the matprotlib font folder. The path depends on the environment, but refer to the following.

/usr/local/lib/python3.8/site-packages/matplotlib/mpl-data/fonts/ttf

If you can't find the lib (library) folder, you can select the library from the Go tab in the Finder or make it always visible from the View options on the View tab. You should reach such a folder.

3. Edit matplotlibrc

Open matplotlibrc in the folder (mpl-data folder) two higher than the ttf folder.

Look for the description "# font.family sans-serif" ... Add "font.family IPAex Gothic" to the line below.

This completes the settings!

Check that the font is displayed normally.

If you get the error "No such font!"

If the following error occurs

UserWarning: findfont: Font family ['IPAexGothic'] not found. Falling back to DejaVu Sans.

It is possible that the font cache remains.

Delete "fontlist ***. Json" in the /Users/username/.matplotlib folder and Please restart Jupyter Notebook. The .matplotlib folder is a hidden folder, so if you don't see it, try "Shift + Cmd +. (Dot)".

in conclusion

The Japanese localization method in the Anaconda environment will come out if you google, but since there was no example in the pure Python environment, I tried to summarize it based on my own experience. I hope you find it useful.

Recommended Posts

[Python] Let's make matplotlib compatible with Japanese
Let's make a GUI with python.
Make matplotlib Japanese compatible in 3 minutes
Let's make a graph with python! !!
Japanese with matplotlib
Let's make a shiritori game with Python
Let's make a voice slowly with Python
Let's make a web framework with Python! (1)
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Heatmap with Python + matplotlib
Let's replace UWSC with Python (5) Let's make a Robot
How to make Linux compatible with Japanese keyboard
Send Japanese email with Python3
Let's run Excel with Python
[Let's play with Python] Make a household account book
Let's make Othello with wxPython
Let's make a simple game with Python 3 and iPhone
Make a fortune with Python
Japanese morphological analysis with Python
Let's write python with cinema4d.
Let's build git-cat with Python
[Super easy] Let's make a LINE BOT with Python.
Let's make a websocket client with Python. (Access token authentication)
Let's create a PRML diagram with Python, Numpy and matplotlib.
Create plot animation with Python + Matplotlib
A python graphing manual with Matplotlib.
Make Echolalia LINEbot with Python + heroku
Make apache log csv with python
[Python] font family and font with matplotlib
Draw Japanese with matplotlib on Ubuntu
Let's make a breakout with wxPython
Let's make Othello AI with Chainer-Part 1-
Let's play with Excel with Python [Beginner]
Make a recommender system with python
Let's do image scraping with Python
Heatmap with Dendrogram in Python + matplotlib
Easy Japanese font setting with matplotlib
Speak Japanese text with OpenJTalk + python
Master the type with Python [Python 3.9 compatible]
Let's make a supercomputer with xCAT
Let's make Othello AI with Chainer-Part 2-
Draw Lyapunov Fractal with Python, matplotlib
When matplotlib doesn't work with python2.7
Lognormal probability plot with Python, matplotlib
Let's analyze voice with Python # 1 FFT
Let's make a web chat using WebSocket with AWS serverless (Python)!
Make the Python console covered with UNKO
[Python] Set the graph range with matplotlib
Generate Japanese test data with Python faker
Let's create a free group with Python
Fractal to make and play with Python
Download Japanese stock price data with python
Notes on doing Japanese OCR with Python
[Introduction to Python] Let's use foreach with Python
Make a partially zoomed figure with matplotlib
Let's read the RINEX file with Python ①
Display Japanese graphs with VS Code + matplotlib
Make PLEN Control Server compatible with ViVi PLEN2
Make pypy submission easier with atcoder-cli (python)
Let's make a simple language with PLY 1