Examples of integration of Python function drawing and advanced algebra (2): Flashpoint function
1: Sine function plot() drawing example, Chinese font display problem
When drawing with matplotlib, if Chinese characters are used during the drawing process, a font warning will appear by default, and Chinese characters will be displayed as boxes or garbled characters.
D:\program_file_worker\python_source_work\SSO\pic\chapter01.py:27: UserWarning: Glyph 26412 (\N{CJK UNIFIED IDEOGRAPH-672C}) missing from current font.
We can see that the warning message prompts "missing from current font", which literally translates to "missing (Chinese characters) in the current font". The approximate meaning is that the default font does not contain Chinese characters.
For this type of problem, the core is to set the font parameters when drawing pictures to include all the characters that need to be used .
# sine functionimport matplotlib.pyplot as plt import numpy as np from pylab import mpl ''' Draw graphs using the matplotlib module Figure is a canvas The linspace(0.5, 3.5, 100) function takes 100 numbers evenly between 0.5 and 3.5 randn(100) means randomly picking 100 numbers from the standard normal distribution ''' #Set Chinese display font mpl.rcParams["font.sans-serif"] = ["SimHei"] #Set normal display symbols mpl.rcParams["axes.unicode_minus"] = False x = np.linspace(0.05, 20, 1000) y = np.sin(x) y1 = np.random.randn(1000) plt.scatter(x, y, label="Sine function drawing example") plt.legend() plt.show()
2: Running results:
D:\program_file_worker\anaconda\python.exe D:\program_file_worker\python_source_work\SSO\pic\chapter01.py
D:\program_file_worker\python_source_work\SSO\pic\chapter01.py:27: UserWarning: Glyph 26631 (\N{CJK UNIFIED IDEOGRAPH-6807}) missing from current font.
plt.show()
D:\program_file_worker\python_source_work\SSO\pic\chapter01.py:27: UserWarning: Glyph 35760 (\N{CJK UNIFIED IDEOGRAPH-8BB0}) missing from current font.
plt.show()
D:\program_file_worker\python_source_work\SSO\pic\chapter01.py:27: UserWarning: Glyph 22270 (\N{CJK UNIFIED IDEOGRAPH-56FE}) missing from current font.
plt.show()
Three: Solution
Add the following two lines of code:
Dynamically setting matplotlibrc in the Python script can also avoid the trouble caused by changing the configuration file. The specific code is as follows: #Set Chinese display font mpl.rcParams["font.sans-serif"] = ["SimHei"] Sometimes, after the font is changed, some characters in the coordinate axis cannot be displayed normally. In this case, the axes .unicode_minus parameter needs to be changed : #Set normal display symbols mpl.rcParams["axes.unicode_minus"] = False
Notice:
rcParams modifies the corresponding font of font.sans-serif or font.family
# The following code sets the font to SimHei (Hellface) globally to solve the problem of displaying Chinese [Windows]
# You can set font.sans-serif or font.family
plt.rcParams['font.sans-serif'] = ['SimHei' ]
# plt.rcParams['font.family']=['SimHei']
# Solve the problem of displaying the negative sign of negative coordinate axis numbers in Chinese fonts
plt.rcParams['axes.unicode_minus'] = False
because mac computers do not have SimHei by default ( bold) font, you can download and install this font or modify it to the system's own font such as Arial Unicode MS, as follows:# The following code sets the font globally to Arial Unicode MS to solve the problem of displaying Chinese [mac]
# You can 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 negative coordinate axis numbers under Chinese fonts
plt.rcParams['axes.unicode_minus'] = False
rc method, in fact Basically equivalent to setting rcParams# Set the font dictionary to SimSun (Chinese New Year), the size is 12 (default is 10)
font = {'family' : 'SimSun',
'size' : '12'}
# Set the font
plt.rc('font', ** font)
# Solve the problem of displaying the negative sign of negative coordinate axis numbers in Chinese font
plt.rc('axes', unicode_minus=False)
Four: Cosine function plot() drawing example
# cosine function import matplotlib.pyplot as plt import numpy as np from pylab import mpl ''' Functional function: shows the trend change of variables Call function: plt.plot(x,y,ls="_",lw=2,label="plot figure" Parameter Description: value on x-axis value on y-axis ls: Line style for line charts lw: line width of line chart label: Mark graphic content label text ''' #Set Chinese display font mpl.rcParams["font.sans-serif"] = ["SimHei"] #Set normal display symbols mpl.rcParams["axes.unicode_minus"] = False x = np.linspace(0.05, 10, 1000) y = np.cos(x) plt.plot(x, y, ls='-', lw=2, label="Cosine function drawing example") plt.legend() plt.show()
Five: Example running results