[PYTHON] What to do when Japanese is not displayed on matplotlib

Environmental setting

Follow the steps below to set up a machine learning environment. http://qiita.com/mix_dvd/items/29dfb8d47a596b4df36d

Obtaining and executing sample code

Let's test to display the graph using the data of PyData.Okinawa. https://github.com/PyDataOkinawa/meetup001/blob/master/PyData.Okinawa%20Meetup%20001%20opendata%20sample.ipynb

$ git clone https://github.com/PyDataOkinawa/meetup001.git

Launch the Jupyter Notebook, open the notebook named "PyData.Okinawa Meetup 001 opendata sample.ipynb" and run the kernel "Restart & Run all".

Looking at the graph displayed at the bottom, the Japanese part is "□".

Unknown.png

Font settings

Download the font from IPA and install it for matplotlib to improve the above situation.

You can download it from the following page. If you cannot download with the command, please download with a browser. http://ipafont.ipa.go.jp/old/ipafont/download.html

Ubuntu

$ sudo apt install unzip
$ wget -O ipafont.zip http://ipafont.ipa.go.jp/old/ipafont/IPAfont00303.php
$ unzip ipafont.zip
$ mv IPAfont00303/*.ttf .pyenv/versions/anaconda2-4.0.0/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/
$ rm .cache/matplotlib/fontList.cache

Mac

First, check the location to clear the font cache. Check the location of the font cache with the following code.

python


import matplotlib
from matplotlib import rc
print(matplotlib.get_cachedir())

In my case, it was "/Users/[username]/.matplotlib".

$ cd
$ curl -o ipafont.zip http://ipafont.ipa.go.jp/old/ipafont/IPAfont00303.php
$ unzip ipafont.zip
$ mv IPAfont00303/*.ttf .pyenv/versions/anaconda2-4.0.0/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/
$ rm .matplotlib/*.cache

Script modification

Change the following parts.

Change before


font = {'family':'Osaka'}
rc('font', **font)

After change


font = {'family':'IPAGothic'}
rc('font', **font)

By the way, if you do not put the following code somewhere, an error will occur, so it is recommended to put it before the above code.

python


from matplotlib import pyplot

Re-execute

Japanese was displayed safely.

Unknown2.png

Recommended Posts

What to do when Japanese is not displayed on matplotlib
Notes on what to do when matplotlib scatter () / scatter3d () does not work
What to do if Japanese language support is not completely installed on Ubuntu 16.04
[Mac OS] What to do when Python is not installed as a framework. Is displayed when import matplotlib is performed.
What to do if Python IntelliSense is not displayed in VS Code on Windows
What to do if the image is not displayed using matplotlib etc. in the Docker container
What to do when a warning message is displayed in pip list
[Beginner] What to do when "[Errno 2] File b'test.csv' does not exist: b'test.csv" is displayed when reading pandas csv
[virtualbox] What to do when [Could not retrieve mirrorlist] appears when yum update is performed on CentOS7
curl: (60) What to do when Issuer certificate is invalid.
What to do if pyenv is not enabled (zsh)
What to do if CERTIFICATE_VERIFY_FAILED occurs when nltk.download () is done on macOS pyhon
What to do if abort is displayed when inputting camera video in OpenCV
[EC2] What to do when selenium is stuck and processing does not proceed
When Pydev is not displayed even after adding Pydev5.2 to Eclipse (2016/8/30)
[Python] Type Error:'WebElement' object is not iterable What to do when an error occurs
What to do when Ubuntu crashes
What to do when python3 type venv does not work well on Raspberry Pi
What to do when is not in the sudoers file.This incident will be reported.
What to do when PyCharm font is strange or garbled
What to do when only the window is displayed and nothing is displayed in pygame Note
What to do if the progress bar is not displayed in tqdm of python
What to do if `pip install matplotlib` fails on Mac
What to do when "Type Error: must be _socket.socket, not socket" appears on GAE
bash: cannot create temp file for here-document: What to do when No space left on device is displayed
What to do when a Missing artifact occurs in a jar that is not defined in pom.xml
What to do when the jupyterlab extension settings are not reflected
What to do if the inode is exhausted on EC2 Linux
What to do when the value type is ambiguous in Python?
NameError: global name'dot_parser' is not defined and what to do when it comes up in python
What to do when the result downloaded via scrapy is in English
[Python] What to do when an error related to SSL authentication is returned
What to do when the warning "The environment is in consistent ..." appears in the Anaconda environment
What to do when "Something is already running at port 8000" is displayed when the develop command of Gatsby is executed.
How to write what to do when an application is first displayed in Qt for Python with Designer
What to do in my case when pyenv install is not possible after upgrading to macOS Big Sur
What to do when PermissionError of tempfile.mkstemp occurs
What is the reason why the basic commands are not displayed in Japanese in man?
What to do when "TypeError: must be string, not int…" appears when using strptime
What to do when "TypeError: data type not understood" appears in python's numpy.zeros
What to do when the graph does not appear in jupyter (ipython) notebook
What to do if (base) is displayed at the beginning of the Mac terminal
What to do if SciPy installation fails on CentOS
What to do when gdal_merge creates a huge file
What to do when raise ValueError, "unsupported hash type"
What to do when "cannot import name xxx" [Python]
pipenv shell is no longer available ... what to do?
When "ERROR: HTTP is not supported." Is displayed in mpsyt
What to do if sys / cdefs.h does not exist
Suspend shutdown on linux When sleep is not possible
What to do when you can't bind CaboCha to Python
What to do if "export" keeps appearing on terminal when trying to put Python on macOS
[Python] What to do when No module named'pyproj.datadir' appears when Exe is done with PyInstaller
What to do if you get an error when importing matplotlib in Python (Mac)
What to do when Python starts up in Anaconda does not come out unexpectedly
What to do if you get an Import Error when importing matplotlib with Jupyter
What to do if you don't want to use Japanese column names when using ortoolpy.logistics_network
What to do if you get the error ʻERR_FEATURE_UNAVAILABLE_ON_PLATFORM` when using ts-node-dev on Linux
What to do when matplotlib gets angry on CentOS saying "I'm using Agg so I can't issue a figure"
What to do if Jupyter Notebook on WSL does not start automatically in your browser
[AWS] What to do when you want to pip with Lambda