1

My code:

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()

Got errors:

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.

enter image description here

Tried some solutions from:

  1. Glyph 23130 missing from current font
  2. How to show Chinese characters in Matplotlib graphs?
  3. official solution: https://matplotlib.org/stable/tutorials/text/text_props.html#text-with-non-latin-glyphs

Is there anything need to install or another to show chinese characters?

Jimmy Wang
  • 67
  • 9

1 Answers1

0

Check available fonts by codes below.

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}')

If you can't find your fonts, try some solutions like here.

Tsing
  • 119
  • 6