Matplotlib画图中文字体乱码设置
一、字体基本设置
win系统会自带一些一体可直接使用
#step0:导入画图工具包 matplotlib
import matplotlib.pyplot as plt
#step1:手动创建一个figure对象,相当于一个空白的画布
figure = plt.figure()
plt.rcParams['font.sans-serif'] = ['SimHei'] # 指定字体
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
二、其他字体:
可直接使用
plt.rcParams['font.sans-serif']=['STSong'] ## 中文宋体
plt.rcParams['font.sans-serif']=['SimHei'] ## 中文黑体
plt.rcParams['font.sans-serif']=['Kaiti'] ## 中文楷体
plt.rcParams['font.sans-serif']=['Lisu'] ## 中文隶书
plt.rcParams['font.sans-serif']=['FangSong'] ## 中文仿宋
plt.rcParams['font.sans-serif']=['YouYuan'] ## 中文幼圆
三、展示一下:
# step0:导入画图工具包 matplotlib
import matplotlib.pyplot as plt
# step1:手动创建一个figure对象,相当于一个空白的画布
figure = plt.figure()
# step2:字体基本设置
plt.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
# step3:在画布上添加一个坐标系,标定绘图位置
axes1 = figure.add_subplot(1, 1, 1)
# step4:准备画图的数据
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [21, 27, 29, 32, 29, 28, 35, 39, 49]
# step5:设置基本信息
axes1.set_xlabel('X轴')
axes1.set_ylabel('Y轴')
axes1.set_title("图片标题")
# step6:画图,设置线条颜色、线型、点标记符
axes1.plot(x, y, color='red', linestyle='-.', marker='*')
# step7:展示
plt.show()
四、运行结果:
完美解决