我想使用Matplotlib对我的力图(由shap.plots.force创建)添加一些修改,例如添加标题、使用紧凑布局等.但是,我try 添加标题,但标题没有显示.知道为什么以及如何使用Matplotlib添加标题吗?

import numpy as np
import shap
import matplotlib.pyplot as plt

myBaseline=1.5
shap_values_0 = np.array([-1, -4, 3])
test_point_0 = np.array([11, 12, 13])
features_names = ['a1','a2','a3']

shap.plots.force(myBaseline,shap_values_0,test_point_0,features_names,matplotlib = 1)
plt.suptitle("This is my title")    # It doesn't show up, why?
fig = plt.gcf()
fig.canvas.draw()
plt.close()

enter image description here

推荐答案

force_plotare中的最后几行:

if show:
    plt.show()
else:
    return plt.gcf()

因此,如果设置为show = False,您可以将准备好的SHAP plot设置为figure对象,并像往常一样根据您的需要进行自定义:

import shap

myBaseline = 1.5
shap_values_0 = np.array([-1, -4, 3])
test_point_0 = np.array([11, 12, 13])
features_names = ["a1", "a2", "a3"]

shap.plots.force(
    myBaseline, shap_values_0, test_point_0, features_names, matplotlib=True, show=False
)
plt.title("This is my title", y=1.75)
plt.show()

enter image description here

Python相关问答推荐

pandas MultiIndex是SQL复合索引的对应物吗?

从收件箱获取特定列中的重复行

Django关于UniqueBindition的更新

解析讨论论坛只给我第一个用户 comments ,但没有给我其他用户回复

如何使用函数正确索引收件箱?

在Python中,如何才能/应该使用decorator 来实现函数多态性?

如何在Python中增量更新DF

阅读Polars Python中管道的函数定义

如何使用上下文管理器创建类的实例?

Python中的函数中是否有充分的理由接受float而不接受int?

Pandas 第二小值有条件

使用SciPy进行曲线匹配未能给出正确的匹配

当使用keras.utils.Image_dataset_from_directory仅加载测试数据集时,结果不同

如何标记Spacy中不包含特定符号的单词?

通过pandas向每个非空单元格添加子字符串

修复mypy错误-赋值中的类型不兼容(表达式具有类型xxx,变量具有类型yyy)

Godot:需要碰撞的对象的AdditionerBody2D或Area2D以及queue_free?

如何指定列数据类型

如何禁用FastAPI应用程序的Swagger UI autodoc中的application/json?

使用Python从rotowire中抓取MLB每日阵容