matplotlib plot with chinese label:UserWarning:当前字体中缺少Glance 25293(\N{CJK已删除IDEOGRAPH-62 CD})

5anewei6  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(186)

我的代码:

import matplotlib
matplotlib.rcParams['font.family'] = ['Source Han Sans TW', 'sans-serif']
matplotlib.__version__  # 3.5.3
import math
from matplotlib import pyplot as plt

size = math.ceil(len(df_attr)** (1/2))
fig = plt.figure()

for i, col in enumerate(df_attr):
    fig.add_subplot(size, size, i + 1)
    df_template_income_tax[col].value_counts().plot(kind="bar", ax=plt.gca(), title=df_attr_eng[i], rot=0)

fig.tight_layout()

出现错误:

findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans.
findfont: Generic family 'sans-serif' not found because none of the following families were found: SimHei
/opt/microsoft/mlserver/9.4.7/runtime/python/lib/python3.7/site-packages/ipykernel_launcher.py:13: UserWarning: Glyph 25293 (\N{CJK UNIFIED IDEOGRAPH-62CD}) missing from current font.

尝试了一些解决方案:

  1. Glyph 23130 missing from current font
  2. How to show Chinese characters in Matplotlib graphs?
    1.官方解决方案:https://matplotlib.org/stable/tutorials/text/text_props.html#text-with-non-latin-glyphs
    有什么需要安装或其他显示汉字?
bqjvbblv

bqjvbblv1#

检查可用的字体代码如下。

import matplotlib.font_manager as fm

# matplotlib only know these fonts
font_list = [f for f in fm.fontManager.ttflist]

# check font names what you want
cjk_list = ['CJK', 'Han', 'CN', 'TW']

for f in font_list:
    if any(s.lower() in f.name.lower() for s in cjk_list):
        print(f'name={f.name}, path={f.fname}')

如果你找不到你的字体,尝试一些解决方案,如here

相关问题