天天看点

Mac 系统下python中文绘图Mac 系统下Python绘图乱码

Mac 系统下Python绘图乱码

问题:博主在使用了Mac系统之后,发现使用Matplotlib绘图的时候,使用中文设计titile or label的时候,最终图形上并没有展现,博主查看了很多解决方案,最终找到了解决方案。

打开Terminal,输入以下代码:

open /Users/zbf/.matplotlib
           

打开的文件夹下有名称为fontlist-v330.json这样的文件,打开该文件可以查看所有Mac系统下的字体以及所对应的名称。

博主设置的中文宋体,英文Times new roman,由于本身matplotlib渲染的英文字体以及数字和Times new roman基本一致,因此可以不做设置,最终设置如下:

from matplotlib import rcParams
config = {
    "font.family": 'serif',
    "font.size": 12, 
    "font.serif": ['Songti SC'],
    "mathtext.fontset": 'stix', 
    'axes.unicode_minus': False 
}
rcParams.update(config)
           

折线图

Mac 系统下python中文绘图Mac 系统下Python绘图乱码