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

相关推荐
YH552698417 小时前
MacBook Air M4 和 M5 怎么选?
macos
MoSTChillax18 小时前
数字人竞品分析(中):核心功能、产品使用与技术能力对比
ide·macos·xcode
码农阿豪1 天前
【金仓数据库征文】Mac开发环境|SpringBoot3 + MyBatisPlus对接KES V9 Docker实战全指南
数据库·macos·docker
赶路人儿2 天前
MacOS 下实现 AI 操控电脑(Computer Use)的思考
人工智能·macos·策略模式
天空'之城2 天前
VMware17.5.0安装macOS13.7.8|Xcode15.0.1+Qt6.5.3开发环境搭建完整教程
macos·虚拟机·vmware tools
00后程序员张2 天前
使用Instruments工具深入分析iOS应用性能与启动时间优化
android·macos·ios·小程序·uni-app·cocoa·iphone
2601_960906722 天前
AI研发加速中式及泛亚洲
人工智能·vscode·macos·sublime text·phpstorm
sg_knight2 天前
Codex CLI 安装全攻略:macOS / Linux / Windows(WSL2)三端实战
linux·windows·macos·openai·ai编程·coding·codex
Teleger3 天前
蓝牙 MAC 扫描匹配器
macos
刘婉晴3 天前
【漏扫工具篇】Mac 系统上 Xray 的安装与使用
macos