Skip to main content

方便地将 Matplotlib 支持中文字体允许您配置图形为“中文宋体、英文 Times New Roman”

Project description

mksci-font

mksci-font 用于方便地将 Matplotlib 支持中文字体允许您配置图形为“中文宋体、英文 Times New Roman”。

安装

使用喜欢的包管理工具安装:

pip install mksci-font

使用方法

配置默认字体设置

要为 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轴标签")

更多用法例子可以见这个笔记本

许可证

本项目基于 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.4.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.4-py3-none-any.whl (10.3 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mksci_font-0.1.4.tar.gz
  • Upload date:
  • Size: 10.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.5.0-1025-azure

File hashes

Hashes for mksci_font-0.1.4.tar.gz
Algorithm Hash digest
SHA256 0fa3cf483123668227c8233faa90c096659ee65ff90ff9db4bdee8f6633ccec1
MD5 16ec9c0b08d423106560821cd497ef1d
BLAKE2b-256 9b881d6d59b5ba7eae8c88091c98a395136d5180a26f2baedb00d5c39d70b872

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mksci_font-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 10.3 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.5.0-1025-azure

File hashes

Hashes for mksci_font-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ed8291250c3c034dedbe9149e12849f025ec5887c5f4ae9ef4b82996b89eb300
MD5 efc5b88ccdc342f70875583ef8ecec7f
BLAKE2b-256 97fa97dda6decc31fdd2b2769cbf6cf64861dcd92e2ba026afcd51073a4318f3

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