使matplotlib的ylabel垂直显示CJK文本

2024-05-28 22:19:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用matplotlib生成带有CJK(尤其是日语)标签的绘图。因为CJK可以垂直写入,所以我希望ylabel以这种方式写入。我在下面找到了一种方法,但是它需要用\n填充每个字符以创建所需的效果。不过,这是哈基

有没有更好的方法,例如,将每个角色旋转90度

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Make a plot with Japanese labels.
"""

import matplotlib.pyplot as plt
from matplotlib import font_manager as fm

# Set fonts so that labels display correctly.
pathToInstall = '/path/to/fonts'
fontFiles = fm.findSystemFonts(pathToInstall)
fontList = fm.createFontList(fontFiles)
fm.fontManager.ttflist.extend(fontList)
fp = fm.FontProperties()
fp.set_family('Noto Sans Mono CJK JP')

fig = plt.figure()
fig.suptitle('こんにちは、世界さん!', FontProperties=fp) # Fine

plt.xlabel('アイウエオ', FontProperties=fp) # Fine
plt.ylabel(
    'あ\nい\nう\nえ\nお', # How can I avoid padding each character with \n?
    FontProperties = fp,
    rotation = 'horizontal',
    verticalalignment = 'center',
    horizontalalignment = 'right'
)

plt.show()

Plot showing output of above code.


Tags: 方法importlabelsmatplotlibaswithfontsplt

热门问题