方便地将 Matplotlib 支持中文字体允许您配置图形为“中文宋体、英文 Times New Roman”
Project description
mksci-font
mksci-font 用于方便地将 Matplotlib 支持中文字体允许您配置图形为“中文宋体、英文 Times New Roman”。
安装
使用喜欢的包管理工具安装:
pip install mksci-font
当前版本要求 Python >= 3.10。
之所以提高最低 Python 版本,是为了跟进 Pillow 的安全修复版本,并避免旧版本传递依赖持续触发安全告警。
从源码开发(uv)
本仓库使用 uv 管理依赖与锁文件。克隆后可在项目根目录执行:
uv sync --extra dev
uv run pytest tests/test_smoke.py
修改 pyproject.toml 中的依赖后,请同步锁文件并更新导出的 requirements.txt(供仅使用 pip 的环境或工具扫描):
uv lock
uv export --no-hashes --no-emit-project -o requirements.txt
使用方法
配置默认字体设置
要为 Matplotlib 配置默认字体设置,可以使用 config_font() 函数。
# 推荐写法:显式传 rc_params
config_font(rc_params={"font.size": 12})
_, ax = plt.subplots(figsize=(4, 1))
ax.text(0.5, 0.5, msg, ha='center', va='center')
plt.show();
针对做图函数修改
对于返回matplotlib.axes的作图函数,可以简单使用 @mksci_font 装饰器,在修改字体的同时,可以将图中元素换成中文。
python
msg = "让 Matplotlib 图件使用 \n “中文宋体、英文 Times New Roman”"
mapping_string = {'Origin title': '替换后的标题'}
@mksci_font(mapping_string, ylabel="覆盖原Y轴标签")
def plot():
_, ax = plt.subplots(figsize=(4, 3))
ax.text(0.5, 0.6, "mksci-font 中文", ha='center')
ax.text(0.5, 0.3, msg, ha='center')
ax.set_ylabel("will be replaced...") # will be replaced by '中文'
ax.set_xlabel("中文 & English & $TeX_{mode}$")
ax.set_title("Origin title")
return ax
ax = plot()
show()
更新现有图形的文本元素
可以使用 update_font() 函数更新已有图像,使用方法与@mksci_font类似:
_, ax = plt.subplots(figsize=(4, 3))
ax.text(0.5, 0.6, "mksci-font 中文", ha='center')
ax.text(0.5, 0.3, msg, ha='center')
ax.set_ylabel("will be replaced...") # will be replaced by '中文'
ax.set_xlabel("中文 & English & $TeX_{mode}$")
ax.set_title("Origin title")
msg = "让 Matplotlib 图件使用 \n “中文宋体、英文 Times New Roman”"
mapping_string = {'Origin title': '替换后的标题'}
update_font(ax, mapping_string, ylabel="覆盖原Y轴标签")
更多用法例子可以见这个笔记本
维护说明
项目显式约束了安全版本的 Pillow,而不是仅依赖 matplotlib 的传递依赖解析结果。
这样可以在保持与 matplotlib 兼容的同时,避免解析到存在已知安全问题的旧版本。
字体资源策略:
- 默认优先使用系统中已安装的
SunTimes字体。 - 如果你希望随包分发字体文件,可将
.ttf/.ttc/.otf放到mksci_font/data/,运行时会自动尝试注册。 - 当系统字体不可用且包内也没有字体文件时,库会抛出明确错误,提示安装字体或补充文件。
依赖解析以 uv.lock 为准;requirements.txt 由 uv export 生成,提交前请与锁文件保持一致。
许可证
本项目基于 MIT 许可证开源。有关详细信息,请参阅 LICENSE 文件。
关于作者
Shuang Song, a scientist who also travels.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mksci_font-1.0.0.tar.gz.
File metadata
- Download URL: mksci_font-1.0.0.tar.gz
- Upload date:
- Size: 10.3 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e2fb77a36723d925e4d0343f7c75413a670777b71f00290b69b05de97b4db10
|
|
| MD5 |
3909c2946171e3a1f539cb1481c8044d
|
|
| BLAKE2b-256 |
b2e7892a93ac032c6e86a979397bbc3666a9f5bbf98104d30fa35f1ea8d78e63
|
File details
Details for the file mksci_font-1.0.0-py3-none-any.whl.
File metadata
- Download URL: mksci_font-1.0.0-py3-none-any.whl
- Upload date:
- Size: 10.3 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a7beb91e633dc7a63bc23d830fe6ce201e51239fb2f5f83690993d259c8ebc5f
|
|
| MD5 |
39a8b731c0f589c7fa1a321a8041e24f
|
|
| BLAKE2b-256 |
27a1d6a20da15d8569b5ea48a63699701ea6f772b835712939858b2380029662
|