Python implements matplotlib to display Chinese characters,
This example describes how to implement matplotlib to display Chinese Characters in Python. We will share this with you for your reference. The details are as follows:
Note]
It may have nothing to do with the topic of this article, but I still want to point out: when using the matplotlib library, the following two import methods are equivalent (I mean equivalent, of course, this can be discussed :)
import matplotlib.pyplot as plt
import pylab as plt
【]
Method 1: FontProperties
Import matplotlib. pyplot as pltfrom matplotlib. font_manager import FontProperties # Step 1 #... font = FontProperties (fname = r "c: \ windows \ fonts \ simsun. ttc ", size = 14) # Step 2 plt. xlabel ("X axis", fontproperties = font) # Step 3 plt. ylabel ("Y axis", fontproperties = font) plt. title ("title", fontproperties = font) plt. show ()
In general, it is ugly.
Method 2: fontproperties
Import matplotlib. pyplot as plt #... plt. xlabel ("X axis") plt. ylabel ("Y axis", fontproperties = "SimSun") # Step 1 () plt. title ("title", fontproperties = "SimHei") # () plt. show ()
Flexibility, another advantage: Global font settings are not contaminated
Method 3: rcParams
Import matplotlib. pyplot as pltplt. rcParams ['font. sans-serif '] = ['simhei'] # Step 1 (replace sans-serif font) plt. rcParams ['axes. unicode_minus '] = False # Step 2 (to solve the problem of displaying the negative number of the coordinate axis )#... plt. xlabel ("X axis") plt. ylabel ("Y axis") plt. title ("title") plt. show ()
Simple usage. Disadvantage: Contaminated global font settings. (So the second step is required)
Method 4: rc
Import matplotlib. pyplot as pltfont = {'family ': 'simhei', 'weight': 'bold', 'SIZE': '16'} plt. rc ('font', ** font) # Step 1 (set more font attributes) plt. rc ('axes ', unicode_minus = False) # Step 2 (To solve the negative number display problem of the coordinate axis )#... plt. xlabel ("X axis") plt. ylabel ("Y axis") plt. title ("title") plt. show ()
Flexible usage. Disadvantage: The Global font is affected (so the second step is required)
[Summary]
It is set only when method 2 is used, and the global font settings are not contaminated, making it more flexible
Method 3 and method 4 do not require hard encoding of the font path, and they are configured at a time and used multiple times for convenience.
[Test code]
Import numpy as npimport matplotlib. pyplot as pltfont = {'family ': 'dfkai-SB', 'weight': 'bold ', 'SIZE': '16'} plt. rc ('font', ** font) # pass in the font dict as kwargsplt. rc ('axes ', unicode_minus = False) x = np. linspace (0, 10,100 0) y = np. sin (x) z = np. cos (x ** 2) plt. figure (figsize = (8, 4) plt. plot (x, y, label = "$ sin (x) $", color = "red", linewidth = 2) plt. plot (x, z, "B --", label = "$ cos (x ^ 2) $") plt. xlabel ("X axis") plt. ylabel ("Y axis") plt. title ("title") plt. ylim (-1.2, 1.2) plt. legend () plt. show ()
Running result:
[Appendix]
English names of some Chinese Fonts
|
SimSun |
Simhei |
SimHei |
|
Microsoft YaHei |
Microsoft is in bold |
Microsoft JhengHei |
Xin |
NSimSun |
New body |
PMingLiU |
Fine body |
MingLiU |
Standard entity |
DFKai-SB |
Imitation Song |
FangSong |
Body |
KaiTi |
Shu |
LiSu |
Youyuan |
YouYuan |
文 |
STXihei |
文 |
STKaiti |
文 |
STSong |
文中 |
STZhongsong |
文 |
STFangsong |
Founder Shu-Ti |
FZShuTi |
Founder Yao Ti |
FZYaoti |
文 cloud |
STCaiyun |
Mandarin amber |
STHupo |
文 |
STLiti |
文 |
STXingkai |
Chinese New Wei |
STXinwei |