在 2x2 网格中绘制形状图

TYL*_*TYL 3 python matplotlib subplot shap

我试图在 2x2 子图中绘制 4 个 Shap 依赖图,但无法使其工作。我已经尝试过以下方法:

fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(10,10))
ax1 = plt.subplot(221)
shap.dependence_plot('age', shap_values[1], X_train, ax=ax1)
ax2 = plt.subplot(222)
shap.dependence_plot('income', shap_values[1], X_train, ax=ax2)
ax3 = plt.subplot(223)
shap.dependence_plot('score', shap_values[1], X_train, ax=ax2)
Run Code Online (Sandbox Code Playgroud)

和这个:

plt.figure(figsize=(10,5))
plt.subplot(1,2,1)
shap.dependence_plot('age', shap_values[1], X_train)
plt.subplot(1,2,2)
shap.dependence_plot('income', shap_values[1], X_train)
plt.subplot(2,2,3)
shap.dependence_plot('score', shap_values[1], X_train)
plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)

它不断将它们绘制在不同的行上,而不是 2x2 格式。

小智 5

您需要将参数传递show=False给依赖图。

fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(10,10))
shap.dependence_plot('age', shap_values[1], X_train, ax=axes[0, 0], show=False)
shap.dependence_plot('income', shap_values[1], X_train, ax=axes[0, 1], show=False)
shap.dependence_plot('score', shap_values[1], X_train, ax=axes[1, 0], show=False)
plt.show()
Run Code Online (Sandbox Code Playgroud)

本笔记本中,您可以阅读有关此论点的以下内容:

''通过传递 show=False 你可以阻止 shap.dependence_plot 调用 matplotlib show() 函数,这样你就可以在最终自己调用 show 之前继续自定义绘图``