总结:matplotlib中三种输出中文方式

摘要

  • 我个人比较喜欢前两种,因为设置是全局的。-。-

code1

import matplotlib


font = {'family' : 'MicroSoft YaHei',
        'weight': 'bold',
        'size': '12',
        }
        
matplotlib.rc("font",**font)

code2

import matplotlib


matplotlib.rc("font",family='MicroSoft YaHei',weight="bold")

code3

from matplotlib import pyplot as plt
from matplotlib import font_manager


my_font = font_manager.FontProperties(fname=r"C:\Windows\Fonts\MSYH.TTC")

# 使用示例:
plt.xlabel("年份",fontproperties=my_font)
plt.ylabel("女朋友多少个)",fontproperties=my_font)
plt.title("从小到大每年交的女朋友人数",fontproperties=my_font)
# 特例
plt.legend(prop=my_font)

补充code3(寻找字体路径):

  • win下:win搜索“字体”点击“设置”,筛选字体为“简体中文”,在点进去,然后查看路径及名字。要配合xticks()的fontproperties参数使用)
  • linux/mac下:fc-list :lang=zh 查看系统自带的中文字体

总结

  • 前两种粘贴即用,第三种需要在显示中文的对应的函数中添加fontproperties参数
  • 前两种实际上是一种,只是不同的表现形式,关键点在于(**font实现了拆包)。
  • 想要给前两种方法添加更多参数的话,看一下源码即可。
发布了55 篇原创文章 · 获赞 3 · 访问量 2749

猜你喜欢

转载自blog.csdn.net/rusi__/article/details/102750780