欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

搞定Python matplotlib.pyplot的中文显示乱码问题

最编程 2024-02-14 11:02:48
...

解决Python matplotlib.pyplot 中文乱码问题

1.

from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # 指定默认字体:解决plot不能显示中文问题
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题

2.

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签SimHei
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
# 黑体(宋体?) SimHei
# 微软雅黑 Microsoft YaHei
# 微软正黑体 Microsoft JHengHei
# 新宋体 NSimSun
# 新细明体 PMingLiU
# 细明体 MingLiU
# 标楷体 DFKai-SB
# 仿宋 FangSong
# 楷体 KaiTi
# 仿宋-GB2312 FangSong_GB2312
# 楷体-GB2312 KaiTi_GB2312

# font.sans-serif : SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
# ‘font.family’ 用于显示字体的名字
# ‘font.style’ 字体风格,正常’normal’ 或斜体’italic’
# ‘font.size’ 字体大小,整数字号或者’large’ ‘x-small’

3.

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"C:\WINDOWS\Fonts\STKAITI.TTF", size=14)

x = np.arange(0, 10, 0.1)
y = x * 2
plt.subplot(2, 2, 1, frameon=False) # 两行一列,位置是1的子图
plt.title("一元一次函数", fontproperties=font)
plt.plot(x, y)
plt.show()
plt.title("一元一次函数", fontproperties=font) #然后设置需要显示的内容中,fontproperties=font
# 系统字体Win7  C:\WINDOWS\Fonts
# 系统字体Win10 C:\Windows\Fonts
# 选中字体右键属性,可以知道字体的名称