Matplotlib 如何输出各种字体
matplotlib 默認(rèn)輸出的是無(wú)襯線字體,因此,修改無(wú)襯線字體的配置即可正確輸出中文:
import matplotlib.pyplot as pltplt.rcParams["font.sans-serif"] = ["STSong"]一般來(lái)說(shuō), matplotlib 會(huì)將字體的配置保存在 ~/.matplotlib/fontlist.json 里,比如隨便找一個(gè) entry 是這樣結(jié)構(gòu)的:
{"fname": "fonts/afm/phvro8an.afm","name": "Helvetica", "style": "italic","variant": "normal","weight": "medium","stretch": "condensed","size": "scalable","__class__": "FontEntry" }name 就表示字體的名稱,fname 是該字體的目錄,可以在該 json 文件里添加新字體。
在 matplotlib 中,plt.title、plt.xlabel、plt.ylabel、plt.text 和 plt.legend 這幾個(gè)函數(shù)是涉及添加文字內(nèi)容的。
修改字體類型或大小的方式,plt.title、plt.xlabel、plt.ylabel、plt.text 相同,使用 fontdict 參數(shù);plt.legend 可以使用
fontdict 是一個(gè)字典,見(jiàn):https://www.tutorialexample.com/understand-matplotlib-fontdict-a-beginner-guide-matplotlib-tutorial/
| family | such as serif, fantasy, Tahoma, monospace,’Times New Roman’ et al. You should notice: you can set font-family or font in this key. |
| color | darkred, red, black |
| weight | ‘light’, ‘normal’, ‘medium’, ‘semibold’, ‘bold’, ‘heavy’, ‘black’ |
| size | 10, 12, .... |
| style | ‘normal’, ‘italic’, ‘oblique’ |
此外,還可以用其它關(guān)鍵詞參數(shù),如 fontsize、fontfamily、fontstyle 來(lái)修改字體相關(guān)參數(shù)。
例:
plt.plot([1, 2, 3, 4]) plt.title("中文字體") plt.xlabel("English Italic", fontdict={"family": "Times New Roman", "style": "italic"}) plt.ylabel("English Regular", fontdict={"family": "Times New Roman", "style": "normal"}) plt.text(x=0.1, y=1.9, s="Random Text", fontsize=16, fontstyle="italic", fontfamily="Times New Roman") plt.text(x=1.9, y=2.5, s="\it{Tex Text} $a^2 + b^2$", usetex=True, fontsize=14) plt.legend(["$\Gamma, \mathit{\Gamma}$ 中文"])結(jié)果如圖:
plt.legend 只提供了 prop 參數(shù)接受一個(gè) matplotlib.font_manager.FontProperties 來(lái)指定字體,或者是一個(gè)類似的字典。
import matplotlib.font_manager.FontProperties as FontPropertiesFontProperties(family=None, style=None, variant=None, weight=None, stretch=None, size=None, fname=None, math_fontfamily=None)plt.legend(["$\Gamma, \mathit{\Gamma}$ 123abc"], prop={"family": "Times New Roman", "size": 16})但這樣的問(wèn)題是,$$ 符號(hào)輸出的時(shí)候,用的是 matplotlib 自帶的對(duì) LaTex 的支持,它支持不是特別完整,不能輸出一些很專門的符號(hào),比如 \mathit{\Gamma}、\mathbb{E},這時(shí)候可以用 plt.rc('text', usetex=True) 設(shè)置全局 LaTex 輸出,程序會(huì)調(diào)用 LaTex 來(lái)渲染這句話,甚至可以調(diào)用宏包來(lái)輸出更復(fù)雜的數(shù)學(xué)公式!
默認(rèn)使用 PDFLaTex,可以調(diào)整參數(shù)改成 XeLaTex 來(lái)獲得對(duì)中文的支持!甚至可以添加導(dǎo)言區(qū)。
import matplotlib as mpl mpl.use("pgf") plt.rcParams.update({"pgf.texsystem": "xelatex","pgf.preamble": "\n".join([r"\usepackage{amsfonts}",r"\usepackage{xeCJK}",r"\usepackage[utf8x]{inputenc}",r"\usepackage[T1]{fontenc}",r"\usepackage{fontspec}",r"\setmainfont{Times New Roman}",r"\usepackage{amsmath}",# r"\setCJKmainfont{STSong}",]), })如果不是要在一句話里同時(shí)輸出中文和復(fù)雜的公式,那么用 PDFLaTex 也足夠了。
比如加入 url 宏包,可以生成帶超鏈接的 pdf 圖片:
plt.figure() plt.plot([1, 2, 3, 4]) plt.title("中文標(biāo)題", usetex=False, fontfamily="STSong") plt.xlabel(r"$\mathbb{E}[X] = \frac{1}{n^2+1}$, \url{http://baidu.com}", usetex=True) plt.ylabel("\\textrm{English Regular}, $mc^2, \mathrm{VaR}$") plt.text(1.5,2.5, "\\textit{Tex Text} \\texttt{monospace} $a^2 + b^2 \mathit{A} \Rightarrow + \mathscr{A}$", usetex=True) # plt.legend(["$\Gamma, \mathit{\Gamma}$"]) plt.savefig('ex3.pdf', bbox_inches="tight") # plt.show()效果如下:
推薦閱讀:
- https://www.cnblogs.com/Renyi-Fan/p/13953276.html
- https://stackoverflow.com/questions/44779531/make-a-label-both-italic-and-bold-style-in-matplotlib-with-stix-font
- https://stackoverflow.com/questions/8376335/styling-part-of-label-in-legend-in-matplotlib
- https://stackoverflow.com/questions/2537868/sans-serif-math-with-latex-in-matplotlib/20709149#20709149
- https://developer.aliyun.com/article/595569
總結(jié)
以上是生活随笔為你收集整理的Matplotlib 如何输出各种字体的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: u盘启动制作装机工具-(u盘启动制作装机
- 下一篇: 6G为什么不被看好?