搞定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
# 选中字体右键属性,可以知道字体的名称
推荐阅读
-
Ubuntu txt 中文显示乱码问题的常规解决方案
-
在 Windows 中彻底解决 Vim/GVim 下中文显示乱码问题的方法
-
如何在Spring Boot与Logback配合下解决控制台中文日志显示乱码的问题
-
如何轻松解决Python mplfinance库最新版本的中文显示乱码问题
-
搞定Python matplotlib.pyplot的中文显示乱码问题
-
搞定!Python数据可视化中文显示成方块的问题解决办法
-
解决JMeter中文字体显示乱码的问题
-
Jmeter新手必看:如何解决Jmeter中的中文显示乱码问题
-
搞定Beyond Compare中文显示乱码的方法
-
搞定IDEA中.properties文件中文显示乱码的解决方案