Skip to content

Commit d0e0055

Browse files
authored
Fix tofu in Chinese figure labels, and give font.family a fallback (#206)
* Restore the CJK font block in three lectures (#205) These three lectures set Chinese text on their figures but never register a CJK font, so matplotlib falls back to DejaVu Sans — which has no CJK coverage — and every Chinese axis label, title and legend entry renders as blank boxes ("tofu"). Nothing errors and a valid PNG is written, so the build stays green and the defect only shows up in the published output. Affected labels include 密度 / KL散度 / JS散度 / 切尔诺夫熵 in divergence_measures.md, 收益率 / 日期 / 时间 / 对数秩 / 对数规模 in kesten_processes.md, and the subplot titles 价值函数 / $s$策略 / $\phi$策略 in jv.md. Adds this edition's standard block to the first code cell that imports matplotlib in each file, immediately after `import matplotlib.pyplot as plt` so it runs before any figure is drawn: import matplotlib as mpl FONTPATH = "fonts/SourceHanSerifSC-SemiBold.otf" mpl.font_manager.fontManager.addfont(FONTPATH) plt.rcParams['font.family'] = ['Source Han Serif SC'] Matches the convention already used by the other 112 lectures here (see aiyagari.md); the path resolves against lectures/, where fonts/SourceHanSerifSC-SemiBold.otf is checked in. Same class of gap as the five lectures fixed in #196 — nothing else in these files is touched. See #205 * Add a DejaVu fallback to font.family, and register the font in two more lectures Setting font.family to a single entry removes matplotlib's fallback, so any character the CJK font lacks renders as a blank box. Source Han Serif SC follows Adobe-GB1, which omits subscript digits (U+2080-2083), the vertical ellipsis (U+22EE), and the lunate/symbol Greek forms (U+03F5 epsilon, U+03D5 phi) — all of which these lectures use in figure labels. Measured on matplotlib 3.11.1 with this repo's own font, rendering a label containing 密钥拆分树, key₀, ⋮, ϵ and ϕ: font.family = ['Source Han Serif SC'] -> 4 missing-glyph warnings font.family = ['Source Han Serif SC', 'DejaVu Sans'] -> 0 Two lectures already carried the block and were silently losing characters to it: career.md sets ylabel='ϵ' (and label='ϵ'), affine_risk_prices.md builds label=f"... (r₁ = ...)". Both are fixed by the fallback alone. This deviates from the single-entry dialect used elsewhere in the edition. That is deliberate: the dialect is missing a fallback, and adding one is strictly an improvement everywhere it appears.
1 parent 84fbaca commit d0e0055

5 files changed

Lines changed: 14 additions & 3 deletions

File tree

lectures/affine_risk_prices.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,7 @@ mystnb:
568568
import matplotlib as mpl # i18n
569569
FONTPATH = "fonts/SourceHanSerifSC-SemiBold.otf" # i18n
570570
mpl.font_manager.fontManager.addfont(FONTPATH) # i18n
571-
mpl.rcParams['font.family'] = ['Source Han Serif SC'] # i18n
572-
571+
mpl.rcParams['font.family'] = ['Source Han Serif SC', 'DejaVu Sans'] # i18n
573572
n_max_1f = 60
574573
maturities_1f = np.arange(1, n_max_1f + 1)
575574

lectures/career.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ import matplotlib.pyplot as plt
6464
import matplotlib as mpl
6565
FONTPATH = "fonts/SourceHanSerifSC-SemiBold.otf"
6666
mpl.font_manager.fontManager.addfont(FONTPATH)
67-
plt.rcParams['font.family'] = ['Source Han Serif SC']
67+
plt.rcParams['font.family'] = ['Source Han Serif SC', 'DejaVu Sans']
6868
6969
from matplotlib import cm
7070
from mpl_toolkits.mplot3d.axes3d import Axes3D

lectures/divergence_measures.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ translation:
6363

6464
```{code-cell} ipython3
6565
import matplotlib.pyplot as plt
66+
import matplotlib as mpl
67+
FONTPATH = "fonts/SourceHanSerifSC-SemiBold.otf"
68+
mpl.font_manager.fontManager.addfont(FONTPATH)
69+
plt.rcParams['font.family'] = ['Source Han Serif SC', 'DejaVu Sans']
6670
import numpy as np
6771
from numba import vectorize, jit
6872
from math import gamma

lectures/jv.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ translation:
5353
from typing import NamedTuple
5454
5555
import matplotlib.pyplot as plt
56+
import matplotlib as mpl
57+
FONTPATH = "fonts/SourceHanSerifSC-SemiBold.otf"
58+
mpl.font_manager.fontManager.addfont(FONTPATH)
59+
plt.rcParams['font.family'] = ['Source Han Serif SC', 'DejaVu Sans']
5660
import scipy.stats as stats
5761
import jax
5862
import jax.numpy as jnp

lectures/kesten_processes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ translation:
7373

7474
```{code-cell} ipython3
7575
import matplotlib.pyplot as plt
76+
import matplotlib as mpl
77+
FONTPATH = "fonts/SourceHanSerifSC-SemiBold.otf"
78+
mpl.font_manager.fontManager.addfont(FONTPATH)
79+
plt.rcParams['font.family'] = ['Source Han Serif SC', 'DejaVu Sans']
7680
import numpy as np
7781
import quantecon as qe
7882
import yfinance as yf

0 commit comments

Comments
 (0)