Chinese garbled characters in matplotlib under Ubuntu

 

This article mainly introduces how to solve the problem of Chinese garbled characters generated by matplotlib under ubuntu. As shown below matplotlib Chinese garbled

Solutions

  • Prepare Chinese fonts
  • matplotlib specifies the font as Chinese font
  • Empty matplotlib to make the configuration take effect

Prepare Chinese fonts

  • Download the Chinese ttf file, or copy it from the Windows operating system. As  in bold (click to download)
  • Double-click to install/ or cp to /usr/share/fonts/truetype/dejavu/
  • Run the command fc-list :lang=zh-cnin the terminal to view the corresponding Chinese fonts 配置名. The corresponding names in bold are SimHeiUbuntu Chinese font list

matplotlib specifies the font as Chinese font

  • Copy the Chinese font files to the fonts/ttf directory of matplotlib. The complete catalog is/usr/local/lib/python*/dist-packages/matplotlib/mpl-data/fonts/ttf
  • Modify the configuration /usr/local/lib/python*/dist-packages/matplotlib/mpl-data/matplotlibrcfile
# 搜索font.family配置项,将其#注释去掉,并将:号后面的值改为字段对应的名字。
font.family         : SimHei

# 搜索axes.unicode_minus配置项,将其#注释去掉,并将:号后面的值改为False
axes.unicode_minus  : False

save and exit

Empty matplotlib to make the configuration take effect

rm ~/.cache/matplotlib -R

final renderings

Unary linear function.png

The test code used in this article

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 10, 0.1)

y = x * 2

plt.title("一元一次函数")
plt.plot(x, y)

plt.show()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326386761&siteId=291194637