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.
OS:Windows 10 VS Code:1.43.2 Extension: Anaconda Extension Pack 1.0.1 Python interpreter: 3.7.3 64-bit
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.
The situation is the same if you try it with Jupyter notebook.
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/.
Unzip this to get the font file (extension tff).
ipaexg.tff is IPAex Gothic ipaexm.tff is IPAex Mincho is.
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.
C:\Users\<username>\.matplotlib\fontlist-v300.json
To delete. Delete the cache file, if any.
Now restart VS Code.
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.
Try it with Jupyter notebook. I was fine.
I would like to graph the data analyzed using scikit-learn.
@ Kanatani28 https://qiita.com/maroKanatani/items/3b080c639395bba7795a @yniji https://qiita.com/yniji/items/3fac25c2ffa316990d0c
Recommended Posts