在尝试在Ubuntu 20.04上运行下面的最小工作示例脚本时,使用matplotlib
中的草书字体会收到matplotlib
警告:
findfont: Font family ['cursive'] not found. Falling back to DejaVu Sans.
这告诉我,我没有任何 matplotlib
想要用于 草书字体族 的草书字体。
在下面的最小失败示例中似乎得到了证实,当 matplotlib
的字体管理器或 fc-list
找不到任何草书字体时。
# on my local machine
$ fc-list : family | grep -i "chancery\|textile\|sand\|script\|felipa\|cursive"
URW Chancery L
Free Chancery
我该如何通过编程的方式在Ubuntu上找到并安装这些字体?我知道我可以在互联网上寻找免费版本,但如果我想将它们安装到Docker镜像中,该如何通过
apt-get
这样的CLI API进行安装?
最小失败示例
对于以下Dockerfile
FROM ubuntu:20.04
RUN apt-get update -y && \
apt-get install -y \
fontconfig \
fonts-dejavu \
fonts-freefont-ttf \
python3 \
python3-dev \
python3-pip \
python3-venv \
vim && \
apt-get -y autoclean && \
apt-get -y autoremove && \
rm -rf /var/lib/apt/lists/*
RUN python3 -m pip install --upgrade --no-cache-dir pip setuptools wheel && \
python3 -m pip install --no-cache-dir "matplotlib~=3.3" && \
python3 -m pip list && \
python3 -c "import matplotlib.pyplot" # generate font list cache
ENV MPLCONFIGDIR /tmp/.config # make writeable to non-root user
WORKDIR /code
COPY example.py example.py
使用example.py
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager
def make_plot(font_family):
fig, ax = plt.subplots()
x = np.linspace(0, 10, 101)
y = np.square(x)
ax.plot(x, y)
ax.semilogy()
ax.set_xlabel("$x$")
ax.set_ylabel("$x^2$")
ax.set_title(f"Default matplotlib settings for {font_family} font family")
return fig, ax
def main():
image_types = ["pdf", "png"]
for font_family in ["sans-serif", "serif", "cursive"]:
plt.rcParams.update({"font.family": font_family})
fig, ax = make_plot(font_family=font_family)
for type in image_types:
fig.savefig(f"family_{font_family}.{type}")
cursive_family = matplotlib.rcParams["font.cursive"]
print(f"\nmatplotlib cursive family: {cursive_family}")
tff_fonts = sorted(
set([font.name for font in matplotlib.font_manager.fontManager.ttflist])
)
afm_fonts = sorted(
set([font.name for font in matplotlib.font_manager.fontManager.afmlist])
)
all_fonts = sorted(set([*tff_fonts, *afm_fonts]))
print(f"\ntff fonts: {tff_fonts}")
print(f"\nafm fonts: {afm_fonts}")
print(f"\nall fonts: {all_fonts}")
found_cursive_fonts = [font for font in cursive_family if font in all_fonts]
print(f"\nfound cursive fonts: {found_cursive_fonts}")
if __name__ == "__main__":
main()
如果使用了构建工具
docker build . \
--pull \
-f Dockerfile \
-t matplotlib-cursive-fonts-question:debug-local
然后运行
$ docker run --rm --user 1000:1000 -v $PWD:$PWD -w $PWD matplotlib-cursive-fonts-question:debug-local /bin/bash -c "python3 /code/example.py"
findfont: Font family ['cursive'] not found. Falling back to DejaVu Sans.
findfont: Font family ['cursive'] not found. Falling back to DejaVu Sans.
matplotlib cursive family: ['Apple Chancery', 'Textile', 'Zapf Chancery', 'Sand', 'Script MT', 'Felipa', 'cursive']
tff fonts: ['DejaVu Math TeX Gyre', 'DejaVu Sans', 'DejaVu Sans Display', 'DejaVu Sans Mono', 'DejaVu Serif', 'DejaVu Serif Display', 'FreeMono', 'FreeSans', 'FreeSerif', 'STIXGeneral', 'STIXNonUnicode', 'STIXSizeFiveSym', 'STIXSizeFourSym', 'STIXSizeOneSym', 'STIXSizeThreeSym', 'STIXSizeTwoSym', 'cmb10', 'cmex10', 'cmmi10', 'cmr10', 'cmss10', 'cmsy10', 'cmtt10']
afm fonts: ['Computer Modern', 'Courier', 'Helvetica', 'ITC Avant Garde Gothic', 'ITC Bookman', 'ITC Zapf Chancery', 'ITC Zapf Dingbats', 'New Century Schoolbook', 'Palatino', 'Symbol', 'Times', 'Utopia', 'ZapfDingbats']
all fonts: ['Computer Modern', 'Courier', 'DejaVu Math TeX Gyre', 'DejaVu Sans', 'DejaVu Sans Display', 'DejaVu Sans Mono', 'DejaVu Serif', 'DejaVu Serif Display', 'FreeMono', 'FreeSans', 'FreeSerif', 'Helvetica', 'ITC Avant Garde Gothic', 'ITC Bookman', 'ITC Zapf Chancery', 'ITC Zapf Dingbats', 'New Century Schoolbook', 'Palatino', 'STIXGeneral', 'STIXNonUnicode', 'STIXSizeFiveSym', 'STIXSizeFourSym', 'STIXSizeOneSym', 'STIXSizeThreeSym', 'STIXSizeTwoSym', 'Symbol', 'Times', 'Utopia', 'ZapfDingbats', 'cmb10', 'cmex10', 'cmmi10', 'cmr10', 'cmss10', 'cmsy10', 'cmtt10']
found cursive fonts: []
并且
docker run --rm --user 1000:1000 -v $PWD:$PWD -w $PWD matplotlib-cursive-fonts-question:debug-local /bin/bash -c "fc-list : family | grep -i 'chancery\|textile\|sand\|script\|felipa\|cursive'"
不返回任何内容。
问题
如何在Ubuntu 20.04上编程查找并安装matplotlib
所需的草书字体?