matplotlib图表显示中文三种办法

一、使用win自带字体(略繁琐,需要每次添加fontproperties=font或legend中prop=font)

1、获取字体在windows系统中的路径

在这里插入图片描述

fname='‪C:/Windows/Fonts/SIMYOU.TTF'
2、代码示例
from matplotlib import pyplot as plt
from matplotlib import font_manager
x=[1]
y=[1]
font = font_manager.FontProperties(fname='C:/Windows/Fonts/SIMYOU.TTF',size=20)
plt.plot(x,y,label='图例')
plt.legend(prop=font)
x_ticks_label = ["{}度".format(i) for i in x]
plt.xticks(x,x_ticks_label,fontproperties=font)
plt.xlabel('温度',fontproperties=font)
plt.show()

在这里插入图片描述

二、使用plt.rcParams动态设置默认的字体

1、主要代码
plt.rcParams['font.sans-serif'] = ['KaiTi']
2、所有代码
from matplotlib import pyplot as plt
x=[1]
y=[1]
plt.rcParams['font.sans-serif'] = ['KaiTi']
plt.plot(x,y,label='图例')
plt.legend()
x_ticks_label = ["{}度".format(i) for i in x]
plt.xticks(x,x_ticks_label)
plt.xlabel('温度')
plt.show()

在这里插入图片描述

三、使用matplotlib.rc()方法

1、获取matplotlib内置字体列表
from matplotlib.font_manager import FontManager
fonts = set(f.name for f in FontManager().ttflist)
for f in sorted(fonts):
    print(f)

在这里插入图片描述

2、找出其中中文字体(这里给出部分的)
Adobe Fan Heiti Std
Adobe Fangsong Std
Adobe Gothic Std
Adobe Heiti Std
Adobe Kaiti Std
Adobe Ming Std
Adobe Myungjo Std
Adobe Song Std
Yu Gothic
YouYuan
FZShuTi
FZYaoTi
FangSong
HYShangWeiShouShuW
KaiTi
LiSu
Microsoft YaHei
STCaiyun
STFangsong
SimHei
3、代码示例
from matplotlib import pyplot as plt
import matplotlib
x=[1]
y=[1]
matplotlib.rc("font",family='SimHei',size=20)
plt.plot(x,y,label='图例')
plt.legend()
x_ticks_label = ["{}度".format(i) for i in x]
plt.xticks(x,x_ticks_label)
plt.xlabel('温度')
plt.show()

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

深耕玩家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值