[Python] Japanese localization of matplotlib on Ubuntu

Introduction

I'm studying crawling and scraping with Python, but I couldn't smoothly translate matplotlib that appeared in the middle of the book into Japanese, so I will summarize it.

environment

Ubuntu virtual environment with virtualbox on Windows 10 Ubuntu 18.04.4 LTS

Background

--Installing matplotlib

Please note that it depends on the environment Execute the following with the command

pip install matplotlib

--Install Japanese fonts on Ubuntu

sudo apt install -y fonts-migmix #For Ubuntu

--Draw a figure with Japanese as the label name

japanese_label.py


import matplotlib
matplotlib.use("Agg")

import matplotlib.pyplot as plt

plt.plot([1,2,3,4,5],[1,2,3,4,5], "bx-", label="Linear function")
plt.plot([1,2,3,4,5],[1,4,9,16,25],"ro--", label="Quadratic function")

plt.xlabel("Value of x")
plt.ylabel("Value of y")

plt.legend(loc="best")
plt.xlim(0, 6)
plt.savefig("japanese_label.png ", dpi=300)

(Reference) [Python Crawling & Scraping-Practical Development Guide for Data Collection and Analysis-](https://www.amazon.co.jp/Python%E3%82%AF%E3%83%AD%E3 % 83% BC% E3% 83% AA% E3% 83% B3% E3% 82% B0-% E3% 82% B9% E3% 82% AF% E3% 83% AC% E3% 82% A4% E3% 83% 94% E3% 83% B3% E3% 82% B0-% E3% 83% 87% E3% 83% BC% E3% 82% BF% E5% 8F% 8E% E9% 9B% 86% E3% 83 % BB% E8% A7% A3% E6% 9E% 90% E3% 81% AE% E3% 81% 9F% E3% 82% 81% E3% 81% AE% E5% AE% 9F% E8% B7% B5 % E9% 96% 8B% E7% 99% BA% E3% 82% AC% E3% 82% A4% E3% 83% 89-% E5% 8A% A0% E8% 97% A4-% E8% 80% 95 % E5% A4% AA / dp / 4774183679 / ref = tmm_other_meta_binding_title_0? _Encoding = UTF8 & qid = & sr =)

If you do this, the figure will be saved in the current directory, but the Japanese part will be displayed as □ as shown below. japanese_label.png

I want to eliminate this

Solution

I referred to this article Draw Japanese with matplotlib on Ubuntu

--Check the location of the installed Japanese fonts

fc-list | grep migmix

/usr/share/fonts/truetype/migmix/migmix-2m-bold.ttf: MigMix 2M:style=Bold
/usr/share/fonts/truetype/migmix/migmix-2p-bold.ttf: MigMix 2P:style=Bold
/usr/share/fonts/truetype/migmix/migu-1m-regular.ttf: Migu 1M:style=Regular
/usr/share/fonts/truetype/migmix/migu-2m-regular.ttf: Migu 2M:style=Regular
/usr/share/fonts/truetype/migmix/migu-1c-regular.ttf: Migu 1C:style=Regular
/usr/share/fonts/truetype/migmix/migu-1p-regular.ttf: Migu 1P:style=Regular
/usr/share/fonts/truetype/migmix/migmix-1p-bold.ttf: MigMix 1P:style=Bold
/usr/share/fonts/truetype/migmix/migmix-2m-regular.ttf: MigMix 2M:style=Regular
/usr/share/fonts/truetype/migmix/migmix-1m-regular.ttf: MigMix 1M:style=Regular
/usr/share/fonts/truetype/migmix/migmix-1p-regular.ttf: MigMix 1P:style=Regular
/usr/share/fonts/truetype/migmix/migmix-2p-regular.ttf: MigMix 2P:style=Regular
/usr/share/fonts/truetype/migmix/migu-2m-bold.ttf: Migu 2M:style=Bold
/usr/share/fonts/truetype/migmix/migu-1p-bold.ttf: Migu 1P:style=Bold
/usr/share/fonts/truetype/migmix/migu-1c-bold.ttf: Migu 1C:style=Bold
/usr/share/fonts/truetype/migmix/migu-1m-bold.ttf: Migu 1M:style=Bold
/usr/share/fonts/truetype/migmix/migmix-1m-bold.ttf: MigMix 1M:style=Bold

It looks like the fonts are installed. Now, check the path of the font you want to use.

--Enable fonts in matplotlib

japanese_label.py


import matplotlib
matplotlib.use("Agg")

#Add the following 4 lines to the previous code
from matplotlib.font_manager import FontProperties
font_path = "/usr/share/fonts/truetype/migmix/migmix-1p-regular.ttf"
font_prop = FontProperties(fname=font_path)
matplotlib.rcParams["font.family"] = font_prop.get_name()

import matplotlib.pyplot as plt

plt.plot([1,2,3,4,5],[1,2,3,4,5], "bx-", label="Linear function")
plt.plot([1,2,3,4,5],[1,4,9,16,25],"ro--", label="Quadratic function")

plt.xlabel("Value of x")
plt.ylabel("Value of y")

plt.legend(loc="best")
plt.xlim(0, 6)
plt.savefig("japanese_label.png ", dpi=300)

Saved figure japanese_label.png

Japanese is displayed

When this still can't be done

This may be due to the (?) Cache file automatically created by matplotlib. I also stumbled here. The matplotlib cache file was in ~ / .cache / matplotlib.

Execute the following command to check the contents and delete what seems to be a cache file

ls ~/.cache/matplotlib/

In my case I was able to delete the cache file with rm ~ / .cache / matplotlib / tex.cache.

Finally

It seems that there are some cases where the cache file does not work as expected. I will remember. By the way, it was my first post. Thank you for reading.

Recommended Posts

[Python] Japanese localization of matplotlib on Ubuntu
Use matplotlib on Ubuntu 12 & Python
Draw Japanese with matplotlib on Ubuntu
build Python on Ubuntu
Japanese localization of Pycharm
Install Python 3.3 on Ubuntu 12.04
Installation of matplotlib (Python 3.3.2)
Build Python3.5 + matplotlib environment on Ubuntu 12 using Anaconda
At the time of python update on ubuntu
Set up Python 3.4 on Ubuntu
Japanese display of matplotlib, seaborn
Japanese localization setting of PyCharm
Set matplotlib font on ubuntu
Put Python 3.x on Ubuntu
Handling of python on mac
Introducing TensorFlow on Ubuntu + Python 2.7
Install Python 3.8 on Ubuntu 18.04 (OS standard)
Python 3.4 pyvenv fails on Ubuntu 14.04 LTS
Prepare Python development environment on Ubuntu
Building a Python environment on Ubuntu
Install Python 3.8 on Ubuntu 20.04 (OS standard)
Introducing Python using pyenv on Ubuntu 20.04
Preparing python using vscode on ubuntu
Installation of Python, SciPy, matplotlib (Windows)
Environment construction of python3.8 on mac
Install Python 3.9 on Ubuntu 20.04 (OS standard?)
Install confluent-kafka for Python on Ubuntu
Install Python 2.7 on Ubuntu 20.04 (OS standard?)
Build Python 3.8 + Pipenv environment on Ubuntu 18.04
Environment construction of "Tello_Video" on Ubuntu
OpenCV3 & Python3 environment construction on Ubuntu
[Python] Visualize overseas Japanese soccer players on a map as of 2021.1.1
Resolve garbled Japanese characters in matplotlib of Jupyter Notebook on Docker
GCP Ubuntu 14.04 and above: Japanese localization of system language and time zone
[Python] Extension using inheritance of matplotlib (NavigationToolbar2TK)
Build Python3 and OpenCV environment on Ubuntu 18.04
Python virtual environment and packages on Ubuntu
Comparison of Japanese conversion module in Python3
Notes on doing Japanese OCR with Python
Created Ubuntu, Python, OpenCV environment on Docker
[Python] Let's make matplotlib compatible with Japanese
About Japanese fonts of matplotlib (for Mac)
Study on Tokyo Rent Using Python (3-1 of 3)
Install pyenv and Python 3.6.8 on Ubuntu 18.04 LTS
Japanese translation: PEP 20 --The Zen of Python
A memo of a tutorial on running python on heroku
Introduction of Python
Install Python3, numpy, pandas, matplotlib, etc. on Windows
Airflow Japanese localization
Build python environment with pyenv on EC2 (ubuntu)
[Python] limit axis of 3D graph with Matplotlib
Python on Windows
twitter on python3
Install python package in personal environment on Ubuntu
[Procedure memo] Install Python3 + OpenSSL locally on Ubuntu
Shebang on Ubuntu 20.04
#Python basics (#matplotlib)
Python development on Ubuntu on AWS EC2 (using JupyterLab)
Use the latest version of PyCharm on Ubuntu
How to run MeCab on Ubuntu 18.04 LTS Python
[Python] Memo to translate Matplotlib into Japanese [Windows]