Ubuntu环境下 matplotlib 图例中文乱码

时间:2021-11-04 08:35:31

最近做了一个最小二乘法的代码编写并用 matplotlib 绘制了一张图,但是碰到了中文乱码问题。简单搜索之后,发现有人总结出了比较好的方案,亲测可行。推荐给大家。

本文前提条件是 已经 安装好 matplotlib  ,  至于 matplotlib 是什么, 怎么安装, 本文不予介绍。

Ubuntu环境下   matplotlib     图例中文乱码

Ubuntu环境下   matplotlib     图例中文乱码

首先在  Windows  盘区下  找到   微软雅黑   的字体文件:

Ubuntu环境下   matplotlib     图例中文乱码

其次,找到 matplotlab  字体库  文件夹    或者   系统字体库文件夹

matplotlab  字体库  文件夹

Ubuntu环境下   matplotlib     图例中文乱码

Ubuntu环境下   matplotlib     图例中文乱码

系统字体库文件夹

Ubuntu环境下   matplotlib     图例中文乱码

把字体文件拷贝到这两个路径下的任何一个都可以。

然后,找到 matplotlib  配置文件

Ubuntu环境下   matplotlib     图例中文乱码

可以直接修改该文件,或者

Ubuntu环境下   matplotlib     图例中文乱码

然后,  修改配置文件。

编辑该配置文件,找到 font.family ,将注释去掉,在下面的 font.sans - serif :Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial,Helvetica, Avant Garde, sans - serif 加上中文字体 Microsoft YaHei

Ubuntu环境下   matplotlib     图例中文乱码

最后,清除缓存

做了上面的更改,发现并没有什么用。

到 ~/.cache/matplotlib下的文件删掉,就ok了。

Ubuntu环境下   matplotlib     图例中文乱码