Display Japanese graphs with VS Code + matplotlib

Last time, I tried "Try running Jupyter with VS Code", but the Japanese display in matplotlib of graph display did not work. .. I've been able to do a lot of research, so I'll record it. It seems that there is not only one method, but I decided to go with this.

1. Environment

OS:Windows 10 VS Code:1.43.2 Extension: Anaconda Extension Pack 1.0.1 Python interpreter: 3.7.3 64-bit

2. Before

import matplotlib.pyplot as plt

flg, ax = plt.subplots()

x = [1, 2, 3, 4, 5]
y = [3, 6, 4, 8, 5]
labels = ['banana', 'pineapple', 'Mandarin orange', 'Strawberry', 'melon']
ax.bar(x, y, tick_label=labels, label='Favorite fruit')
ax.legend()

plt.show()

When I run the above source, the place I want to display in Japanese becomes a long square tofu. image.png

The situation is the same if you try it with Jupyter notebook. image.png

3. Correspondence

3-1. Get font file

You can use the fonts that are pre-installed in the OS, but I want to use the same fonts in an environment such as Linux, so I decided to use IPAex Gothic. Download from https://ipafont.ipa.go.jp/. image.png

Unzip this to get the font file (extension tff). image.png

ipaexg.tff is IPAex Gothic ipaexm.tff is IPAex Mincho is.

3-2. Arrangement of font files

Find the matplotlib configuration file.

import matplotlib as mpl
print(mpl.matplotlib_fname())

It was here for my environment.

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\mpl-data\matplotlibrc

As a relative path from the above ../fonts/ttf That is,

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\mpl-data\fonts/ttf

Place the font file ipaexg.ttf in.

3-3. Settings on the matplotlib side

C:\Users\<username>\.matplotlib\fontlist-v300.json

To delete. Delete the cache file, if any.

Now restart VS Code.

4. After-sales

To explicitly specify the font in the first source

plt.rcParams['font.family'] = 'IPAexGothic'

Is inserted.

import matplotlib.pyplot as plt

plt.rcParams['font.family'] = 'IPAexGothic'
flg, ax = plt.subplots()

x = [1, 2, 3, 4, 5]
y = [3, 6, 4, 8, 5]
labels = ['banana', 'pineapple', 'Mandarin orange', 'Strawberry', 'melon']
ax.bar(x, y, tick_label=labels, label='Favorite fruit')
ax.legend()

plt.show()

It went well. Japanese is displayed. image.png

Try it with Jupyter notebook. I was fine. image.png

5. What I want to do after this

I would like to graph the data analyzed using scikit-learn.

6. Qiita's senior who was helpful

@ Kanatani28 https://qiita.com/maroKanatani/items/3b080c639395bba7795a @yniji https://qiita.com/yniji/items/3fac25c2ffa316990d0c

Recommended Posts

Display Japanese graphs with VS Code + matplotlib
Japanese with matplotlib
Python with VS Code (Windows 10)
Japanese display of matplotlib, seaborn
Debug Python with VS Code
Animate multiple graphs with matplotlib
Try running Jupyter with VS Code
Easy Japanese font setting with matplotlib
Easy to draw graphs with matplotlib
Install python with mac vs code
Two ways to display multiple graphs in one image with matplotlib
Write SVG graphs with matplotlib on heroku
Pass PYTHONPATH in 1 minute with VS Code
How to display python Japanese with lolipop
[Python] Let's make matplotlib compatible with Japanese
Display markers above the border with matplotlib
VS Code Pylint is annoying with import! !! !! !!
[Jupyter Notebook memo] Display kanji with matplotlib
(Mac) How to display Japanese with Matplotlib and Seaborn At MacOS Sierra
I tried Flask with Remote-Containers of VS Code
I want to display multiple images with matplotlib.
Python (Windows 10) Virtual Environment / Package with VS Code
VS Code settings
Debug with VS Code using boost python numpy
Wrap and display Japanese sentences well with pyglet
Animation with matplotlib
How to display images continuously with matplotlib Note
Use Python in Anaconda environment with VS Code
Animation with matplotlib
Histogram with matplotlib
Animate with matplotlib
Use Docker development container conveniently with VS Code
QR code display
Make your Python environment "easy" with VS Code
Build a python execution environment with VS Code
[Python] Customize Colormap when drawing graphs with matplotlib
Code for TensorFlow MNIST Begginer / Expert with Japanese comments
Try touching the micro: bit with VS Code + Python
Allow real-time code checking in Python development with VS Code
VS Code settings for developing in Python with completion
(For those unfamiliar with Matplotlib) Tips for drawing graphs with Seaborn
It's too troublesome to display Japanese with Vim's python3.
Display the graph while changing the parameters with PySimpleGUI + Matplotlib
Enable intellisense for external libraries with Pipenv + VS Code
Remote debug Django environment created with docker-compose with VS Code
Display Matsuya coupon (QR code) with Pythonista for iOS
Strengthen with code test ⑦
2-axis plot with Matplotlib
Strengthen with code test ③
Japanese input with pyautogui
Strengthen with code test ⑤
Strengthen with code test ④
Heatmap with Python + matplotlib
Band graph with matplotlib
Speaking Japanese with OpenJtalk
Learn with Cheminformatics Matplotlib
Strengthen with code test ②
Real-time drawing with matplotlib
3D display with plotly
Various colorbars with Matplotlib
3D plot with matplotlib