Under using Ubuntu, training model in the process of dealing with data path in Chinese, in the process of terminal display, garbled words, triggered a series of problems, the dog blood under the online to find the solution, found that more disorderly, some also not too good, I adjusted a available solution, hope to be of help in this paper. There are many online tutorials, but it took a long time to get them right. For the record.

1. Problem description

>>> Import matplotlib >>> print matplotlib.matplotlib_fname() / home/ubuntu/anaconda3 / lib/python3.6 / site – packages/matplotlib/MPL – data/matplotlibrc PS: after installed the two matplotlib library. So there’s a problem here. Sudo apt-get install python-matplotlib A common user installation using PIP install matplotlib. Sudo apt-get remove python-matplotlib sudo apt-get remove python-matplotlib sudo apt-get remove python-matplotlib

2. Cause analysis and solution

The Chinese characters in Matplotlib are garbled because there is no SimHei font library in the system. You need to download The SimHei font and configure Matplotlib.

If we find a Chinese font on the system and set it to default in Matplotlib, we don’t need to download it anymore.

from matplotlib.font_manager import FontManager
fm = FontManager()
mat_fonts = set(f.name for f in fm.ttflist)
print(mat_fonts)
Copy the code

Arial Unicode MS is the default font for Matplotlib.

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
Copy the code

With the above two lines of code, without any other modifications, the normal display of Chinese.

Permanent solution

First the fonts in the Windows directory simhei. The vera.ttf copied to/home/ubuntu/anaconda3 / PKGS/matplotlib – 2.2.2 – py36h0e671d2_1 / lib/python3.6 / site – packages Fonts /matplotlib/mpl-data/fonts/ TTF /

“` #font.family : sans-serif #font.sans-serif : simhei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif … Unicode_minus: False #axes. Unicode_minus: False #axes. Unicode_minus: False #axes

# -*- coding:utf-8 -*- from matplotlib import pyplot as PLT plt.title(u' display Chinese ') plt.show() # -*- coding:utf-8 -*- from matplotlib import pyplot as PLT plt.title(u' display Chinese ') plt.show()Copy the code

If the Chinese characters in the following figure are displayed, the change is successful.

Finally, a link to simhei.ttF is attached:

Blog.csdn.net/github_3393…