macos或windows中 matplotlib中文显示(matplotlib字体常见使用)

1. 显示中文

1.1 Windows

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

每次都搜,每次都不记!


1.2 Macos

import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

plt.rcParams['font.sans-serif']=['Songti SC'] #用来正常显示中文标签
# 或者是下面这个,宋体和仿宋字体,都可以用。
plt.rcParams['font.sans-serif']=['STFangsong'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

参考:MAC Matplotlib 画图中文乱码处理


1.3 二者通用

plt.rcParams['font.sans-serif']=['SimHei','Songti SC','STFangsong']
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

2. matplotlib使用文件路径代表的字体

2.1 全局设置

由于在服务器上操作,所以上传了一个字体,但是无法把这个字体加到系统字体中

参考:Add custom fonts to Matplotlib

from matplotlib import font_manager

font_dirs = ['/content/drive/MyDrive/OpenMMLab/']
font_files = font_manager.findSystemFonts(fontpaths=font_dirs)

for font_file in font_files:
    font_manager.fontManager.addfont(font_file)
# set font
plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus']=False  # 用来正常显示负号

这种好像有点点麻烦,如果只是某一个图要用,不是多个图,可以用下面的局部设置

2.2 局部设置

参考:Using a ttf font file in Matplotlib

from pathlib import Path

import matplotlib as mpl
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

fpath = Path(mpl.get_data_path(), "fonts/ttf/cmr10.ttf")
ax.set_title(f'This is a special font: {fpath.name}', font=fpath)
# 在每个用到中文字体的地方,显式声明字体路径
ax.set_xlabel('This is the default font')
plt.show()

或者更简单的写法,参考:stackoverflow.com-how to set up a custom font with custom path to matplotlib global font?

fname='/home/user1/myapp/font/myfont.ttf'
myfont=fm.FontProperties(fname=fname)
ax1.set_title('title test',fontproperties=myfont)

3. 有关字体的其他知识

如果想知道自己系统都可以使用哪些字体,可以参考以下内容

3.1 设置特定的字体系列及字体样式

  • 可以明确设置为给定字体样式(例如,“serif”、“sans-serif”或“monospace”)选择哪个字体系列。比如设置一种字体系列 (Tahoma) 用于 sans-serif 字体样式。
rcParams['font.family'] = 'sans-serif'

and for the font.family you set a list of font styles to try to find in order:
# 可以为上面设置的字体家族设置一个字体列表,会按照顺序查找(如果这个字体没有,就会顺着找下一个)
rcParams['font.sans-serif'] = ['Tahoma', 'DejaVu Sans',
                               'Lucida Grande', 'Verdana']

参考自:Configuring the font family

3.2 查看当前支持的字体属性

import matplotlib.font_manager
matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')

参考自:How to get a list of all the fonts currently available for Matplotlib?
其实上面返回的内容,就是系统字体。和自己直接打开系统字体库的结果是一样的。
在这里插入图片描述


如果是linux系统(亲测macos也可以),也可以使用fc-list命令查看当前系统中含有的字体,例如:

# 查看支持的中文字体
fc-list :lang=zh family

# 返回以下内容,就可以在上面填写了
圆体\-简,圓體\-簡,Yuanti SC
Arial Unicode MS
Yuppy TC,雅痞\-繁
手札体\-简,手札體\-簡,Hannotate SC
STKaiti
手札体\-繁,手札體\-繁,Hannotate TC
宋体\-繁,宋體\-繁,Songti TC

可以直接看到这个字体的完整名称,但是字体属于的类型(衬线/非衬线),就需要自己判断


在这里插入图片描述

import matplotlib.pyplot as plt

# 查看当前默认的字体系列
plt.rcParams['font.family']
> ['sans-serif']

# 查看sans-serif字体系列中目前使用的字体样式
plt.rcParams['font.sans-serif']
> ['STFangsong']

在这里插入图片描述
以上字体类型分别表示:

  • serif:衬线字体(衬线字体每个笔画宽度粗细不一样),一般用于新闻报纸的文字排版。如Times, Times New Roman, Georgia.
    在这里插入图片描述

  • ‘sans’, ‘sans serif’, ‘sans-serif’:非衬线字体,一般用于计算机屏幕上文本的显示。如Verdana, Arial Black, Trebuchet MS, Arial, Geneva.
    在这里插入图片描述

  • monospace:等宽字体,指的是字符间有固定宽度的字体,这些字体主要用于显示软件代码示例。如Courier, Courier New, Andale Mono.
    在这里插入图片描述

  • fantasy:玄幻字体/艺术字,指的是包含某种风格的装饰性的字体。 如Last NINJA, Impact.
    在这里插入图片描述

  • cursive:草书/手写字体,指的是类似手写的字体。如Comic Sans, Apple Chancery.
    在这里插入图片描述

参考自:Default Font
字体类型参考:css 日语字体,css字体

3.3 在matplotlib中展示latex

参考:Writing mathematical expressions

plt.title("r'$s(t) = \mathcal{A}\mathrm{sin}(2 \omega t)$'")
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

吨吨不打野

解决了问题,觉得还行就给点

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

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

打赏作者

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

抵扣说明:

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

余额充值