惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
MyScale Blog
MyScale Blog
雷峰网
雷峰网
博客园 - 叶小钗
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 三生石上(FineUI控件)
云风的 BLOG
云风的 BLOG
V
V2EX
宝玉的分享
宝玉的分享
酷 壳 – CoolShell
酷 壳 – CoolShell
N
Netflix TechBlog - Medium
Vercel News
Vercel News
美团技术团队
人人都是产品经理
人人都是产品经理
The Cloudflare Blog

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
How to Plot Unicode Characters with Matplotlib
2018-04-08 · via jdhao's digital space

This is a post which follows up my previous post on how to use Chinese characters with Matplotlib.

Introduction#

If we use some Unicode characters when plotting with Matplotlib, for example, character (Unicode code point is U+2739), you will find that the character may not show up in the rendered image. The reason is simple: the default font used by Matplotlib does not support this Unicode character.

In order to plot this Unicode character, we need to do two things. Firstly, we need to find a font which supports this character. Secondly, we need to tell Matplotlib to choose this font for rendering the character.

Find a valid font#

Since this character may not belong to a certain language. It is trickier to find which font supports it. According to post here, we can use the Python package fontTools to check if a character exists in a certain font.

First, we need to install fontTools. If you are using pip, use the following command to install fontTools,

If you are using conda, use

conda install -c conda-forge fonttools

to install this package.

Then, we can use the following script to show a list of fonts which contain this Unicode character.

from fontTools.ttLib import TTFont
import matplotlib.font_manager as mfm

def char_in_font(Unicode_char, font):
    for cmap in font['cmap'].tables:
        if cmap.isUnicode():
            if ord(Unicode_char) in cmap.cmap:
                return True
    return False

uni_char =  u"✹"
# or uni_char = u"\u2739"

font_info = [(f.fname, f.name) for f in mfm.fontManager.ttflist]

for i, font in enumerate(font_info):
    if char_in_font(uni_char, TTFont(font[0], fontNumber=0)):
        print(font[0], font[1])

The above script will print a list of font path along with their corresponding font names. All these fonts support the queried character. Sample output on my system is shown below:

/home/jdhao/util/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans-Oblique.ttf DejaVu Sans
/home/jdhao/util/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSansMono.ttf DejaVu Sans Mono
/home/jdhao/util/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf DejaVu Sans
/home/jdhao/util/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans-Bold.ttf DejaVu Sans
/home/jdhao/util/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSansMono-Bold.ttf DejaVu Sans Mono
/home/jdhao/util/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans-BoldOblique.ttf DejaVu Sans
/usr/share/fonts/dejavu/DejaVuSansCondensed-BoldOblique.ttf DejaVu Sans
/usr/share/fonts/dejavu/DejaVuSansCondensed-Bold.ttf DejaVu Sans
/usr/share/fonts/dejavu/DejaVuSansCondensed.ttf DejaVu Sans
/usr/share/fonts/dejavu/DejaVuSansCondensed-Oblique.ttf DejaVu Sans
/usr/share/fonts/gnu-free/FreeSerif.ttf FreeSerif
/usr/share/fonts/opensymbol/opens___.ttf OpenSymbol

Plot this character#

In order to plot this character with Matplotlib, we need to use FontProperties class in Matplotlib to find this font. Then we can use this font in the plotting command:

import matplotlib.pyplot as plt
import matplotlib.font_manager as mfm

font_path = '/usr/share/fonts/gnu-free/FreeSerif.ttf'
prop = mfm.FontProperties(fname=font_path) # find this font

# use the font in plotting command
plt.text(0.5, 0.5, s=uni_char, fontproperties=prop, fontsize=20)
plt.show()

In the above code, variable font_path is the path of a font supporting the Unicode character.

We can also use the font name to look up a font,

import matplotlib.pyplot as plt
import matplotlib.font_manager as mfm

font_path = '/usr/share/fonts/gnu-free/FreeSerif.ttf'
prop = mfm.FontProperties(family='OpenSymbol')
plt.text(0.5, 0.5, s=uni_char, fontproperties=prop, fontsize=20)
plt.show()

Output image is shown below


References#