matplotlib设置一张图片中,显示中英不同字体:中文宋体和英文Times New Roman(macos、windows)

windows系统

第1步:下载组合字体

中文宋体和英文Times New Roman链接:SongNTR

第2步:将新字体的ttf文件放入matplotlib字体文件夹中

获取位置方式:

import matplotlib
import os

mpl_data_dir = matplotlib.get_data_path()
font_dir = os.path.join(mpl_data_dir, 'fonts')

print("Matplotlib 默认字体目录:", font_dir)

我的位置在:

D:\applications\anaconda\envs\nlp38\Lib\site-packages\matplotlib\mpl-data\fonts\ttf\

其中:nlp38是我的python环境名称,或者就是自己下载的python路径

在这里插入图片描述

第3步:修改matplotlibrc文件

将262行和270行都解注释,并且在第270行添加字体:SongNTR。
(❗注意:具体行数不一定是这两行,根据下图中的位置找到)

获取位置方式:

import matplotlib
print(matplotlib.matplotlib_fname())

我的位置在:

D:\applications\anaconda\envs\nlp38\Lib\site-packages\matplotlib\mpl-data\matplotlibrc

在这里插入图片描述

在这里插入图片描述

第4步:修改.matplotlib缓存文件 fontlist-v330.json

获取文件位置方式:

import matplotlib
import os

cache_dir = matplotlib.get_cachedir()
print("缓存目录:", cache_dir)

# 假设文件名是 fontlist-v330.json(版本号可能不同)
fontlist_file = os.path.join(cache_dir, 'fontlist-v330.json')
print("找到字体缓存文件:", fontlist_file)

我的位置在:

C:\Users\14221\.matplotlib

打开后插入以下内容:(windows系统)

{
      "fname": "fonts\\ttf\\SongNTR.ttf",
      "name": "SongNTR",
      "style": "normal",
      "variant": "normal",
      "weight": 700,
      "stretch": "normal",
      "size": "scalable",
      "__class__": "FontEntry"
    },

在这里插入图片描述

第5步:重启python,重新进入python即可,或者重新打开IDE

第6步:matplotlib设置字体代码

import matplotlib.pyplot as plt
# 显示中文
plt.rcParams['font.sans-serif'] = ['SongNTR'] ##设置字体
plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示为方块的问题

在这里插入图片描述

mac系统(与windows思路类似,步骤也一样,只需要重点区分文件路径即可,二者路径不一样)

第1步:下载组合字体

中文宋体和英文Times New Roman链接:SongNTR

第2步:将新字体的ttf文件放入matplotlib字体文件夹中

获取位置方式:

import matplotlib
import os

mpl_data_dir = matplotlib.get_data_path()
font_dir = os.path.join(mpl_data_dir, 'fonts')

print("Matplotlib 默认字体目录:", font_dir)

自己下载的python路径或者anaconda的python路径,我的位置在:

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/mpl-data/fonts/ttf

在这里插入图片描述

第3步:修改matplotlibrc文件

将262行和270行都解注释,并且在第270行添加字体:SongNTR
(❗注意:具体行数不一定是这两行,根据下图中的位置找到)

获取位置方式:

import matplotlib
print(matplotlib.matplotlib_fname())

我的位置在:

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/mpl-data

在这里插入图片描述

在这里插入图片描述

第4步:修改.matplotlib缓存文件 fontlist-v330.json

获取文件位置方式:

import matplotlib
print(matplotlib.get_cachedir())  #假设是/home/ddd/.cache/matplotlib

我的位置在:

/Users/liyongbo/.matplotlib

打开后插入以下内容:(mac系统,路径分隔符为:/

{
      "fname": "fonts/ttf/SongNTR.ttf",
      "name": "SongNTR",
      "style": "normal",
      "variant": "normal",
      "weight": 700,
      "stretch": "normal",
      "size": "scalable",
      "__class__": "FontEntry"
    },

在这里插入图片描述

第5步:重启python,重新进入python即可,或者重新打开IDE

第6步:matplotlib设置字体代码

import matplotlib.pyplot as plt
# 显示中文
plt.rcParams['font.sans-serif'] = ['SongNTR'] ##设置字体
plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示为方块的问题

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

githubcurry

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

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

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

打赏作者

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

抵扣说明:

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

余额充值