(未解决)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,图还是画不出来一点

相关推荐
青州从事52143 分钟前
20260108【mac】【brew】【docker】安装
macos·docker·eureka
centor7 小时前
国际版 UnitySetup-Android-Support 安装 Mac 设备
android·macos
旭日跑马踏云飞7 小时前
【Mac】实用技巧-在新窗口中打开当前文件夹
macos
林疏safe9 小时前
CAD2026最新mac的安装教程收集免费分享
macos
Digitally9 小时前
如何高效安全地将 iPhone 与 Mac 同步
安全·macos·iphone
程序员允诺9 小时前
办公 WiFi 二次隔离下的远程桌面解决方案实践(Mac → Win11)
macos
hudawei99610 小时前
win和Mac在创建python虚拟环境,启动环境等操作的异同
windows·python·macos·虚拟环境
2501_9160088910 小时前
没有 Mac 如何在 Windows 上创建 iOS 应用描述文件
android·macos·ios·小程序·uni-app·iphone·webview
提笔忘字的帝国20 小时前
【教程】macOS 如何完全卸载 Java 开发环境
java·开发语言·macos
数据雕塑家1 天前
【网络故障排查实战】多台机器互ping异常:MAC地址冲突引发的网络“薛定谔猫“现象
网络·macos