You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用SHAP分析模型时刻度标签减号字形缺失问题求助

解决SHAP瀑布图减号缺失(Glyph 8722缺失)问题

以下是几个亲测有效的解决方案,按优先级排序:

1. 指定支持Unicode负号的字体

默认字体可能不包含U+2212(MINUS SIGN)字符,直接替换为带该字符的字体即可:

import matplotlib.pyplot as plt
import shap

# 优先选DejaVu Sans(Matplotlib默认备用字体,支持全字符),也可替换为Arial、SimHei等
plt.rcParams['font.sans-serif'] = ['DejaVu Sans', 'Arial', 'SimHei']
plt.rcParams['axes.unicode_minus'] = False

# 生成瀑布图
shap.waterfall_plot(shap_explainer_values[4652])
plt.show()

2. 强制替换图中Unicode负号为ASCII减号

如果换字体无效,直接修改图表中的文本内容,把Unicode负号换成普通ASCII减号:

import shap
import matplotlib.pyplot as plt

def replace_minus_signs():
    # 获取当前图表对象
    fig = plt.gcf()
    # 遍历所有文本元素替换负号
    for text_element in fig.findobj(match=plt.Text):
        original_text = text_element.get_text()
        text_element.set_text(original_text.replace('\N{MINUS SIGN}', '-'))

# 生成瀑布图
shap.waterfall_plot(shap_explainer_values[4652])
# 替换负号
replace_minus_signs()
plt.show()

3. 彻底清理Matplotlib缓存并重启内核

之前的缓存删除操作可能未生效,需彻底清理后重启Python内核:

import matplotlib
import shutil
import os

cache_path = matplotlib.get_cachedir()
if os.path.exists(cache_path):
    shutil.rmtree(cache_path)

执行完上述代码后,重启你的Python内核,再重新运行绘图代码。

4. 调整SHAP/Matplotlib版本

部分版本组合存在字体渲染bug,可尝试切换到稳定版本:

# 安装指定版本
pip install shap==0.41.0 matplotlib==3.7.1

内容的提问来源于stack exchange,提问作者Stockard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 06:10:18