Python用matplotlib畫圖無法顯示中文及Font family ['sans-serif'] not found解決辦法

原文出處:https://blog.csdn.net/sinat_40875078/article/details/104326855

1、在終端python 的環境下查看字體路徑

import matplotlib    
print(matplotlib.matplotlib_fname())

在這裏插入圖片描述

2.下載SimHei字體

以下我給出了網盤鏈接
鏈接:https://pan.baidu.com/s/1PWC1fIDVBoxvo_yxYTAm6A
密碼:4kzu

並前往指定目錄:/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf,將下載好的SimHei移動到該目錄下。

3.修改matplotlibrc文件

修改/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc,修改內容如下:

 font.family         : sans-serif   
 # 去掉前面的#     
 font.sans-serif     : SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif  
 # 去掉前面的#,並在冒號後面添加SimHei
 axes.unicode_minus  : False
 # 去掉前面的#,並將True改爲False

4.清除matplotlibrc緩存

在終端python 的環境下,輸入如下指令,查看matplotlib的字體緩存路徑:

import matplotlib
matplotlib.get_cachedir()

然後使用rm -rf +路徑刪除緩存
在這裏插入圖片描述

然後重啓軟件即可

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章