Matplotlib中文显示异常解决方案

在这里插入图片描述
中文显示异常处理:
设置myfont来规定字体

from matplotlib import font_manager
myfont = font_manager.FontProperties(fname="C:\Windows\Fonts\msyh.ttc")

fname是自己电脑的字体路径,具体寻找方法如下:
默认字体存放路径:

C:\Windows\Fonts

路径下有个fontList.json文件。里面就有你系统带的字体

在这里插入图片描述
超多字体!双击选择字体,右击属性直接复制路径就可以用
在这里插入图片描述
在这里插入图片描述
使用我们的myfont

plt.xticks(list(x)[::3],_xtick_labels[::3],rotation = 45,fontproperties=myfont)

在这里插入图片描述
ok啦
完整源码

from matplotlib import pyplot as plt
import random
import matplotlib
from matplotlib import font_manager
# font = {'family' : 'MicroSoft YaHei',
#               'weight' : 'bold',
#               'size'   : 'larger'}
# matplotlib.rc("font",**font)
# matplotlib.rc("font",family='MicroSoft YaHei',weight="bold")

#

myfont = font_manager.FontProperties(fname="C:\Windows\Fonts\msyh.ttc")

x = range(0,120)
y = [random.randint(20,35) for i in range(120)]

plt.figure(figsize=(15,10),dpi=80)

plt.plot(x,y)

#调整x轴刻度
_x = list(x)[::3]
_xtick_labels = ["10点{}分".format(i) for i in range(60)]
_xtick_labels += ["11点{}分".format(i) for i in range(60)]
#取步长,数字和字符串一一对应
plt.xticks(list(x)[::3],_xtick_labels[::3],rotation = 45,fontproperties=myfont)#旋转的度数

#添加描述信息
plt.xlabel("时间",fontproperties=myfont)
plt.ylabel("温度 单位(°C)",fontproperties=myfont)
plt.title("十点到十二点每分钟的气温变化情况",fontproperties=myfont)

plt.show()
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值