Skip to main content

mpl-tc-fonts

mpl-tc-fonts

🇹🇼 A package to solve the problem of "Tofu" in your matplotlib plots whenever you're trying to use Traditional Chinese characters in labels or texts.

Open In Colab

Installation

Install mpl-tc-fonts with pip:

# Install from GitHub Repository for latest commit
$ pip install git+https://github.com/Hsins/mpl-tc-fonts.git

The package installer pip will install this package from the mpl-tc-fonts to your python environment. Please see the FAQ section for more information and troubleshooting.

Usage

Quick Start

The easiest way to use is just import the packages after installation and the Traditional Chinese characters are properly displayed in your plots.

import matplotlib.pyplot as plt
import mpl_tc_fonts

names = ['分類 A', '分類 B', '分類 C']
values = [1, 10, 100]

plt.figure(figsize=(9, 3))

plt.subplot(131)
plt.bar(names, values)
plt.subplot(132)
plt.scatter(names, values)
plt.subplot(133)
plt.plot(names, values)
plt.suptitle('分類資料圖')
plt.show()

The default font is 思源宋體(Noto Serif CJK TC).

More Features

import mpl_tc_fonts

# Copy the cwTeX fonts into matplotlib folder
font_tool.load_font('cwtex', 'copy')

# Set "cwTeX Q Kai" to be the display font
font_tool.set_font('cwTeX Q Kai')

# Print out current font in use
font_tool.show_font_setting()

# Print out the list of the avaiable font-family name
print(font_tool.scan_font('國'))

There are some functions to help users qucik setup the fonts:

  • mpl_tc_fonts.load_font(folder, method) would install the given fonts.
    • folder can be noto (default) or cwtex
    • method can be link (default) or copy
  • mpl_tc_fonts.set_font(font) would set the given font to display the texts. There are 7 different fonts in this package.
    • Noto Sans CJK TC: 思源黑體
    • Noto Serif CJK TC: 思源宋體
    • cwTeX Q Ming: cwTeX 中明體
    • cwTeX Q Kai: cwTeX 中楷體
    • cwTeX Q Yuan: cwTeX 中圓體
    • cwTeX Q Fangsong: cwTeX 仿宋體
    • cwTeX Q Hei: cwTeX 粗黑體
  • mpl_tc_fonts.scan_font(char) return the list of avaiable fonts in the FontList of matlibplot. The given char is used to check whether glyph could be found in that font or not.
  • mpl_tc_fonts.show_font_setting() print out current font in use.

FAQ

License

Licensed under the GPL-3.0 License, Copyright © 2020-present Hsins.

Metadata

Release files for mpl-tc-fonts 1.0.1

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

Source distribution (sdist)

Source distribution for mpl-tc-fonts 1.0.1
File Size Uploaded
mpl-tc-fonts-1.0.1.tar.gz 17.9 kB Details

Release files / mpl-tc-fonts-1.0.1.tar.gz

Download URL mpl-tc-fonts-1.0.1.tar.gz
Size 17.9 kB
Tags Source
SHA-256 checksum
How to use checksums
5992b11efeddc3d4f04b6a5e04a1789c07e2876ace8b7121f03193d7b0b3256c
BLAKE2b-256 checksum
How to use checksums
3e67dd4ab3ab5ab7c1ded492f6211cb59ea66129b3b0f238be0ba7f64dc06ed6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.0.post20201006 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.9

Release history Release notifications | RSS feed

This release

1.0.1 This release

1 release file

1.0.0

1 release file

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