字体问题

字体问题


方法1:

第1步:导入font_manager模块FontProperties类
from matplotlib.font_manager import FontProperties
第2步:指向中文字体库TTF(TrueTypeFont)
ft = FontProperties(fname="D:Byp绝对路径...SimHei.ttf", size=14)
第3步:引用字体库
plt.title(f'{title} 台风路径图', font=ft, fontsize=15)


方法2:

import matplotlib.pyplot as plt
plt.rcParams['axes.unicode_minus'] = False  # 显示减号
plt.rcParams['font.sans-serif'] = ['SimHei']  # 设置中文字体
plt.title("风云四号A星 ", fontsize=12)

import matplotlib as mpl
Simsun = FontProperties(fname="绝对路径.../SimHei.ttf")
Times = FontProperties(fname="绝对路径.../times.ttf")
config = {
"mathtext.fontset": 'stix',
}
mpl.rcParams.update(config)
mpl.rcParams['axes.unicode_minus'] = False

plt是代表画图的库。库内的配置(configuration)是固定好的,但有时我们想要修改plt的配置参数来满足画图需求。可用plt.rcParams['配置参数']=[修改值]进行修改,rcParams即runtime configuration parameters运行配置参数。

官网有设置说明:Customizing Matplotlib with style sheets and rcParamsrcParams的参数列表

There are 3 ways to customize Matplotlib:
1、Setting rcParams at runtime.
2、Using style sheets.
3、Changing your matplotlibrc file.


字体

在rcParams有关字体的设置多达11种:

font.cursive
font.family
font.fantasy
font.monospace
font.sans-serif
font.serif
font.size
font.stretch
font.style
font.variant
font.weight
西方国家字母体系分为两类:serif 以及 sans serif。serif,衬线,截线(部分印刷体的西文字母顶端或底部的短线)。sans,无,没有。

中文字体中的宋体就是一种最标准的 serif 字体,衬线的特征非常明显。字形结构也和手写的楷书一致。因此宋体一直作为最适合的正文字体之一。不过由于强调横竖笔画的对比,在远处观看的时候横线就被弱化,导致识别性的下降。
黑体、幼圆、雅黑则属于sans serif 字体。
在英文中,常用的 serif 字体为Times New Roman,常用的 sans serif 字体为Arial,Tahoma。

```如果字体多处设置相同,可以用以下方法:
font = {'family' : 'Times New Roman',
'weight' : 'normal',
'size'   : 20,
}
ax.setxlabel('X axis', font)


在学术科研论文里面,通常要求中文格式_宋体,英文格式为_新罗马(Times New Roman)。



LaTex常用数学符号(pdf

下图是示例((源码))



LaTex常用数学符号




汉字问题


其实主要是使用matplotlib作图的时候,汉字显示为乱码。

在Linux环境下,findfont: Font family [‘sans-serif’] not found的错误,原因是SimHei字体缺失,

解决的办法如下:

(1)下载Simhei字体
(2)将字体文件移动到对应文件夹
在terminal或jupyter notebook中输入以下命令,得到matplotlib字体的路径

>>import matplotlib
>>print(matplotlib.matplotlib_fname())

路径目录为:******/opt/python/3.8.13/lib/python3.8/site-packages/matplotlib/mpl-data/fonts/ttf

(3)删除字体缓存文件

首先一定要要删除matpotlib的缓存文件(删除该缓存文件后即生效)。其实就是进入home目录,然后进入cd  ~/.cache/matplotlib,将里面的json文件删除,见下图:

修改matplotlib配置文件,(有教程说要修改配置文件)后打开matplotlibrc文件,在font.sans-serif后面的字体改为SimHei,并且修改axes.unicode_minus解决显示横杠问题

font.family         : sans-serif
font.sans-serif : SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
axes.unicode_minus,将True改为False,作用就是解决负号'-'显示为方块的问题
有教程说要“重启一下Python内核”,貌似也不用就成功了。


Last update: 2023-01-20|Pageview:32
Research Blog: EC | EC_INFO | EC_WORK |


BypResearch