QuantEcon / QuantEcon/lecture-python.zh-cn

Three lectures render Chinese plot labels as tofu — no matplotlib font configuration

Open
#205 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TeX
Stars
5
Forks
4
Avg merge
14h 55m
Merged PRs (30d)
9

Description

Three lectures set Chinese text on matplotlib figures but carry no font configuration, so matplotlib falls back to DejaVu Sans, which has no CJK coverage. The labels render as tofu — blank rectangles — in a valid PNG, and nothing fails.

Lecture Chinese plot text How it broke
divergence_measures.md 7 sites, e.g. plt.ylabel('密度'), axes[0].set_xlabel('KL散度 KL(f, g)') never had a block; labels hand-added 2025-09-12, after the blanket font pass
kesten_processes.md 4 sites, e.g. ax.set_ylabel("收益率"), ax.set_xlabel("日期") regression — block deleted by resync da7b176 (#190), labels kept
jv.md via titles = [r"$s$策略", r"$\phi$策略", "价值函数"] block stripped by resync

Fix

Add the edition's standard block to each, above first plotting use:

FONTPATH = "fonts/SourceHanSerifSC-SemiBold.otf"
mpl.font_manager.fontManager.addfont(FONTPATH)
plt.rcParams['font.family'] = ['Source Han Serif SC']

Note fonts/, not _fonts/ — this edition keeps the asset at lectures/fonts/SourceHanSerifSC-SemiBold.otf and the other 103 configured lectures all use that path.

Context

Not yet visible to readers: this edition last published 2025-11-06 and the breakage is post-dated, so the live site still serves pre-resync content. That is timing, not safety — it ships on the next publish.

Root cause and the recommendation against reverting to usetex are in the program review: project-translation reports/2026-07-24-cjk-font-rendering-review.md. Engine side is action-translation#107 — the resync drops i18n adaptations. A further ~12 lectures here use matplotlib with English labels and no config, which becomes tofu the moment anyone localises a label.

CI cannot currently catch any of this: the missing-glyph warning is raised in the kernel subprocess, so -W never sees it.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Inspect divergence_measures.md, kesten_processes.md, and jv.md, comparing their plotting sections with the 103 lectures that already use the edition’s standard font block. Check the asset at lectures/fonts/SourceHanSerifSC-SemiBold.otf and render the affected figures. Done means all three lectures configure that font before plotting and their Chinese labels render without tofu.

Written by the indexing model from the issue text.

Assessment

Tech stack
markdown, matplotlib, python
Domain
data-visualization, documentation
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.