Skip to main content

中文文档

Python package PyPI version contributions welcome

mplfonts

The mplfonts is a Python package and command-line tool that allows you to manage your Matplotlib fonts. It provides an easy solution to the "tofu" problem that arises when plotting with CJK (Chinese, Japanese, Korean) languages.

Installation

You can use pip to install mplfonts.

$ pip install mplfonts

Quickstart

You can easily solve the "tofu" problem with a single command:

$ mplfonts init

After that, try using Matplotlib to plot an image with CJK text; it should display normally.

Now you can enjoy it.

Usage

This package not only solves the "tofu" problem, but also provides a convenient way to manage Matplotlib fonts. When you install mplfonts, it comes with several open-source CJK fonts, including:

  • Noto Sans Mono CJK SC
  • Noto Serif CJK SC
  • Noto Sans CJK SC
  • Source Han Serif SC
  • Source Han Mono SC

Once you have finished the initialization step, you can use the following code in your script to switch fonts:

from mplfonts import use_font

use_font('Noto Serif CJK SC')

# write your plotting code below

If you want to set up custom fonts, you can install them with the following command:

$ mplfonts install --update <your font file path>

Furthermore, you can install all the fonts in a directory by running the following command:

$ mplfonts install --update <your directory path>

Unsure of which fonts are available? No problem, simply use the command mplfonts list to see a list of all available fonts.

$ mplfonts list
DejaVu Sans Display:
/Users/clarmylee/Miniconda3/envs/mplfonts/lib/python3.6/site-packages/matplotlib-3.3.4-py3.6-macosx-10.9-x86_64.egg/matplotlib/mpl-data/fonts/ttf/DejaVuSansDisplay.ttf
---------------
DejaVu Sans:
/Users/clarmylee/Miniconda3/envs/mplfonts/lib/python3.6/site-packages/matplotlib-3.3.4-py3.6-macosx-10.9-x86_64.egg/matplotlib/mpl-data/fonts/ttf/DejaVuSans-BoldOblique.ttf
---------------
Source Han Mono SC:
/Users/clarmylee/Miniconda3/envs/mplfonts/lib/python3.6/site-packages/matplotlib-3.3.4-py3.6-macosx-10.9-x86_64.egg/matplotlib/mpl-data/fonts/ttf/SourceHanMonoSC-Regular.otf

...

This command will show the names and source files of the fonts, which can then be passed as arguments to the use_font function.

By default, running mplfonts init will generate a new matplotlibrc file to replace your current one. This new file will contain cache configuration settings that support CJK rendering. Here is the content of the new matplotlibrc file:

font.family:  sans-serif
font.sans-serif: Noto Sans CJK SC Regular, 思源等宽, Noto Serif CJK SC, 思源宋体, Noto Sans Mono CJK SC Regularsans-serif
axes.unicode_minus: False

By default, Noto Sans CJK SC Regular is the preferred font. If you install custom fonts from another source and want them to have first priority, add them to the leftmost end of the font.sans-serif list. Then, run $ mplfonts updaterc <your matplotlibrc path> to set it as your global cache configuration.

Fonts Source

You can access some open-source fonts from Google and Adobe. Here are some resources:

Release files for mplfonts 0.0.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mplfonts 0.0.11
File Size Uploaded
mplfonts-0.0.11.tar.gz 85.0 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for mplfonts 0.0.11
File Interpreter ABI Platform
mplfonts-0.0.11-py3-none-any.whl Python 3 none any Details

Total release size: 170.0 MB

Release files / mplfonts-0.0.11.tar.gz

Download URL mplfonts-0.0.11.tar.gz
Size 85.0 MB
Tags Source
SHA-256 checksum
How to use checksums
c0ec4baf4bda9aa06df8357adf574886cf9af571740739e5c95a75ebac82b0d7
BLAKE2b-256 checksum
How to use checksums
97d3d5ddd74baf7185cd18e1e2bf83691f711220e9feb3a80a7ddb3d9ff69587
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / mplfonts-0.0.11-py3-none-any.whl

Download URL mplfonts-0.0.11-py3-none-any.whl
Size 85.0 MB
Tags Python 3
SHA-256 checksum
How to use checksums
b5e05ba7fd9a59cae48d1db41f657f50e6765e6f04b8d78504438b93a66b6e13
BLAKE2b-256 checksum
How to use checksums
41d5fbe61c8f2bd81a17db1b8258e588d8a2a77b10fdc28afc5ffa1b4d0756be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

0.0.11 This release

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page