Skip to main content

方便地将 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() 函数。

python

# 同时还可以修改字号,以及其它任何 rcParams 支持的属性
config_font({"font.size": 12})

_, ax = plt.subplots(figsize=(4, 1))
ax.text(0.5, 0.5, msg, ha='center', va='center')
plt.show();

U73Adi

针对做图函数修改

对于返回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()

WbZq1I

更新现有图形的文本元素

可以使用 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 兼容的同时,避免解析到存在已知安全问题的旧版本。

依赖解析以 uv.lock 为准;requirements.txtuv export 生成,提交前请与锁文件保持一致。

许可证

本项目基于 MIT 许可证开源。有关详细信息,请参阅 LICENSE 文件。

关于作者

Shuang Song, a scientist who also travels.

Buy Me A Coffee

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

mksci_font-0.1.5.tar.gz (10.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mksci_font-0.1.5-py3-none-any.whl (10.3 MB view details)

Uploaded Python 3

File details

Details for the file mksci_font-0.1.5.tar.gz.

File metadata

  • Download URL: mksci_font-0.1.5.tar.gz
  • Upload date:
  • Size: 10.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.4

File hashes

Hashes for mksci_font-0.1.5.tar.gz
Algorithm Hash digest
SHA256 e4c85a7d899a0dab93066adbbafb1189f0b39d1f9f1bb5a2cdb547d658f33d69
MD5 dd1fca39c7ec46e51f399778c4fe2074
BLAKE2b-256 4bd5500c94484ef7baf116fda490ed6069f59c92a862f1a466cdd82de9162313

See more details on using hashes here.

File details

Details for the file mksci_font-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: mksci_font-0.1.5-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

Hashes for mksci_font-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 0d8149edf90af5f39e7281678572364fed6a2c66cb4a8a2fd9980afdb44266f6
MD5 afea8551b5b849d10fcb74f2c96e0f61
BLAKE2b-256 88d98bd63943fc83a74acd265b1e39e338538ba79ab2378511d3248d5a5f57a4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page