Matplotlib如何顯示中文


問題:matplotlib不能渲染中文

想設定為中文字體,網上搜索的方法幾乎都是下面這樣,已經把字體拷貝到了程序目錄下了,然而並沒有生效

 plt.rcParams [ font.sans-serif'] = ['SimHei.ttf'] 

解決

設置字體路徑字體名

import matplotlib.font_manager as font_manager
from matplotlib import pyplot as plt

font_dirs = ['你的字體路徑']
font_files = font_manager.findSystemFonts(fontpaths=font_dirs)
font_list = font_manager.createFontList(font_files)
font_manager.fontManager.ttflist.extend(font_list)
plt.rcParams['font.family'] = '你的字體名

下面是本人用的代碼

import matplotlib.font_manager as font_manager
font_dirs = ['/Users/chenqionghe/me/project/python/python-test/jupyter/myfonts']
font_files = font_manager.findSystemFonts(fontpaths=font_dirs)
font_list = font_manager.createFontList(font_files)
font_manager.fontManager.ttflist.extend(font_list)
plt.rcParams['font.family'] = 'SimHei'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM