Table of contents
1. Chinese font display problem:
2.1 Set the global font in the drawing code
2.2 Set the local font in the drawing code
2.3 Modify the default configuration font of native characters
1. Chinese font display problem:
C:\Users\86157\anaconda3\lib\site-packages\IPython\core\pylabtools.py:132: UserWarning: Glyph 26376 (\N{CJK UNIFIED IDEOGRAPH-6708}) missing from current font.fig.canvas.print_figure (bytes_io, **kw) When drawing with matplotlib, if Chinese is used in the drawing process, a font warning will appear by default, and Chinese characters will be displayed in the form of boxes or garbled characters. For details, see the following case:
# 设置线宽
plt.figure(figsize=(10,4))
plt.plot(x, y, linewidth=4)
# 设置图表标题,并给坐标轴添加标签
plt.title("月份/成交额折线图", fontsize=12)
plt.xlabel("月份", fontsize=12)
plt.ylabel("成交额", fontsize=12)
plt.grid(True, linestyle='-', alpha=0.5)
# 设置坐标轴刻度标记的大小
plt.tick_params(axis='both',
labelsize=12)
for a, b in zip(x, y):
plt.text(a, b, b, ha='center', va='bottom', fontsize=10)
plt.show()
Chinese garbled:
C:\Users\86157\anaconda3\lib\site-packages\IPython\core\pylabtools.py:132: UserWarning: Glyph 26376 (\N{CJK UNIFIED IDEOGRAPH-6708}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
We can see that " missing from current font " is prompted in the warning message . The literal translation is "missing (Chinese characters) in the current font", which probably means that the default font does not contain Chinese characters.
For this kind of problem, the core is to set the font parameters when drawing pictures to include all the characters that need to be used .
2. Several solutions
When we solve the problem of Chinese character display, there are two kinds of solutions and multiple ways: solution 1, set the global character display font in the drawing code; solution 2, set the local font in the drawing code; solution 3, modify the default configuration of local characters font.
2.1 Set the global font in the drawing code
Dynamically set matplotlibrc in the Python script, which can also avoid the trouble caused by changing the configuration file. The specific code is as follows:
from pylab import mpl
# 设置显示中文字体
mpl.rcParams["font.sans-serif"] = ["SimHei"]
Sometimes, after the font is changed, some characters in the coordinate axis will not be displayed normally. In this case, the parameters of axes.unicode_minus need to be changed:
# 设置正常显示符号
mpl.rcParams["axes.unicode_minus"] = False
Notice:
rcParams modify font.sans-serif or font.family corresponding font
# The following code sets the font to SimHei (black body) globally to solve the problem of displaying Chinese [Windows] # Set font.sans-serif or font.family can be plt.rcParams['font.sans-serif'] = ['SimHei' ] # plt.rcParams['font.family']=['SimHei'] # Solve the problem of displaying the negative sign of the negative coordinate axis in Chinese font plt.rcParams['axes.unicode_minus'] = FalseSince the mac computer does not have the SimHei (黑体) font by default, you can download and install the font or modify it to a font that comes with the system such as Arial Unicode MS , as follows:
# The following code sets the font to Arial Unicode MS globally to solve the problem of displaying Chinese [mac] # It is possible to set font.sans-serif or font.family plt.rcParams['font.sans-serif'] = ['Arial Unicode MS '] # plt.rcParams['font.family']=['Arial Unicode MS'] # Solve the problem of displaying the negative sign of the negative coordinate axis under the Chinese font plt.rcParams['axes.unicode_minus'] = FalseThe rc method is basically equivalent to setting rcParams
# Set the font dictionary to SimSun (Arial), the size is 12 (the default is 10) font = {'family' : 'SimSun', 'size' : '12'} # Set the font plt.rc('font', ** font) # Solve the problem of displaying the negative sign of the negative axis of the Chinese font plt.rc('axes', unicode_minus=False)
!! For example: rc('lines', linewidth=2, color='r')
equivalent to the following:
rcParams['lines.linewidth'] = 2 rcParams['lines.color'] = 'r'
Case display:
2.2 Set the local font in the drawing code
FontProperties object, in this case there is no need to deal with the negative sign
import matplotlib.pyplot as plt
import numpy as np
# 引入matplotlib字体管理 FontProperties
from matplotlib.font_manager import FontProperties
# 设置我们需要用到的中文字体(字体文件地址)
my_font = FontProperties(fname=r"c:\windows\fonts\SimHei.ttf", size=12)
# Data for plotting
t = np.arange(-1.0, 1.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
plt.plot(t, s)
# 设置 x轴名称字体
plt.xlabel('时间 (s)', fontproperties=my_font)
plt.ylabel('voltage (mV)')
# 设置 标题字体
plt.title('简单的标题', fontproperties=my_font)
plt.show()
FontProperties object
Set the fontproperties parameter directly, and in this case, there is no need to deal with the negative sign problem
import matplotlib.pyplot as plt
import numpy as np
# Data for plotting
t = np.arange(-1.0, 1.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
plt.plot(t, s)
# 设置 x轴名称字体 黑体 SimHei
plt.xlabel('时间 (s)', fontproperties='SimHei')
plt.ylabel('voltage (mV)')
# 设置 标题字体 微软雅黑 Microsoft YaHei
plt.title('简单的标题', fontproperties='Microsoft YaHei')
plt.show()
2.3 Modify the default configuration font of native characters
In addition to the above font settings in the code, we can also directly modify the default configuration font of the local characters, but in this case the code is only applicable to the local machine.
- Step 1: Download the SimHei font (or other fonts that support Chinese display)
- Step 2: Install fonts
- Under windows and mac: double-click to install
- Step 3: Modify the configuration file matplotlibrc to find the configuration file path:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
print(matplotlib.matplotlib_fname()) # 此处输出的就是配置文件的路径
Open the file to view:
Modify the file content to:
font.family: sans-serif
font.sans-serif: SimHei
axes.unicode_minus: False
Add fonts that support Chinese characters in the following places #font.sans-serif such as: SimSun (宋体), or directly modify #font.family: SimSun
Considering the display problem of negative signs under Chinese fonts, synchronization needs to be modified # axes.unicode_minus: False
Modify True to False