(未解决)macOS matplotlib 中文是方框

reference:

Mac OS系统下实现python matplotlib包绘图显示中文(亲测有效)_mac plt 中文值-CSDN博客

module 'matplotlib.font_manager' has no attribute '_rebuild'解决方法_font_manager未解析-CSDN博客

# 问题描述(笑死 显而易见

# solve

找到本地字体所在的路径

python 复制代码
>>> import matplotlib
>>> print(matplotlib.matplotlib_fname())
/CtripSpider-master/venv2/lib/python3.11/site-packages/matplotlib/mpl-data/matplotlibrc

聚焦搜索mlp-data文件夹

预览字体,如果有中文字体Heiti TC

python 复制代码
plt.rcParams['font.sans-serif']= ['Heiti TC']#防止中文乱码
plt.rcParams['axes.unicode_minus']=False#解决负号'-'显示为方块的问题

如果没有修改

  • 下载中文字体

Download SimHei Font - Free Font Download - FontPalace.com

拷贝到该文件夹下

  • matplotlibrc文件里的三个参数

查看当前系统中的字体

python 复制代码
font_list=sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])
for i in font_list:
  print(i)

没有SimHei

  • 告诉python 添加了新字体
python 复制代码
from matplotlib.font_manager import _rebuild
------rebuild()

or(反正都报错 两种都一样的

python 复制代码
import matplotlib.font_manager as font_manager
font_manager._rebuild()

出问题了,报错没有 _rebuild 属性

  • 替代方案 都是为了清除缓存 重新载入
python 复制代码
import shutil
import matplotlib

shutil.rmtree(matplotlib.get_cachedir())
  • 重启内核

- 继续查看字体有没得装上 查看当前matplotlib所有可用字体

python 复制代码
for font in font_manager.fontManager.ttflist:
    print(font)
python 复制代码
import matplotlib.font_manager
matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')

有宋体的

出现这一大堆字,没一个支持中文,没一个我安装的字体

SimHei字体文件拽过来(拽不过来,可能因为是系统盘叭

但是偶然发现了系统里的中文字体,改改试试

蒽,一如既往,还是不对

以为是因为字体文件的后缀的问题。(.ttc .ttf)

累了 又找了一段代码 查看系统的字体

python 复制代码
import matplotlib.font_manager
from IPython.core.display import HTML

def make_html(fontname):
    return "<p>{font}: <span style='font-family:{font}; font-size: 24px;'>{font}</p>".format(font=fontname)

code = "\n".join([make_html(font) for font in sorted(set([f.name for f in matplotlib.font_manager.fontManager.ttflist]))])

HTML("<div style='column-count: 2;'>{}</div>".format(code))

# print(code)

print(HTML("<div style='column-count: 2;'>{}</div>".format(code)))

错误,真的改不出来一点。算了。

=====

笑死,还是回到最开始的问题

module 'matplotlib.font_manager' has no attribute '_rebuild'解决方法

python 复制代码
import shutil
import matplotlib
import matplotlib.font_manager as font_manager


shutil.rmtree(matplotlib.get_cachedir())


for font in font_manager.fontManager.ttflist:
    print(font)

SimHei字体有了

真不放心,又用第二种方法检查SimHei是不是真的安装好了

python 复制代码
import matplotlib

import matplotlib.font_manager as font_manager

print(matplotlib.matplotlib_fname())

font_list=sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])
for i in font_list:
  print(i)

这次真的在了

6,图还是画不出来一点

相关推荐
shark-chili1 小时前
关于AI辅助编程的认知
数据库·人工智能·redis·macos·缓存
zwd20057 小时前
Mac 上写英文遇到不会的词,有哪些工具?输入法、翻译软件、写作助手怎么选
人工智能·macos·效率工具·英语写作·输入法·deepl·翻译工具
33三 三like8 小时前
Jx-Mac 本地部署过程总结
macos
大象AI共学1 天前
想在手机上读 Obsidian?Mac → iPhone / 安卓的零成本方案
macos·智能手机·iphone
sysinside2 天前
如何创建可引导的 macOS Golden Gate 27 安装介质
macos
Dr.Buho2 天前
macOS 27 升级教程:兼容机型、三种安装方法与降级提示
经验分享·macos
sysinside2 天前
macOS Golden Gate 27.0 (26A428) 正式版 Boot ISO 原版可引导映像下载
macos
黑科技iOS上架3 天前
Cocos应用App Store4.3被拒怎么自查
macos·ios·objective-c·cocoa·审核·ios混淆·cocos混淆
典典分享指南3 天前
AI 配图的排版规范
macos·docker·github·vim·visual studio
:-)4 天前
修改mac电脑用户名
macos