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.
Ubuntu virtual environment with virtualbox on Windows 10 Ubuntu 18.04.4 LTS
--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.
I want to eliminate this
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 is displayed
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
.
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