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

相关推荐
FreeBuf_6 小时前
高度复杂的macOS DigitStealer采用多阶段攻击逃避检测
macos·m2芯片
ai_xiaogui13 小时前
Mac苹果版Krita AI一键安装教程:AIStarter+ComfyUI零基础部署全流程(X86/ARM双架构)
arm开发·人工智能·macos·comfyui·一键部署·ai绘画教程·kritaai
晚枫~1 天前
不同浏览器在Windows和Mac上的跨域配置方法
windows·macos
denggun123451 天前
Xcode的App Thinning Size Report分析包体积
macos·蓝桥杯·xcode
醇氧1 天前
MAC安装 Notepad Next
macos
我的golang之路果然有问题2 天前
mac配置 unity+vscode的坑
开发语言·笔记·vscode·macos·unity·游戏引擎
alanesnape2 天前
Mac 目录树结构与基础 Linux 指令指南
macos
Digitally3 天前
解决“Move to iOS 卡在准备中”的 9 种有效方法
macos·ios·cocoa
影魅周影3 天前
解决mac端pycharm执行allure命令报错:returned non-zero exit status 127
ide·macos·pycharm
2501_927541093 天前
CameraBag Mac英文 照片视频滤镜编辑工具
macos