python - 在 2x2 网格中绘制形状图

标签 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)

还有这个:

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()

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

最佳答案

您需要将参数 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()

This notebook您可以阅读以下有关此论点的内容:

''通过传递 show=False 你可以阻止 shap.dependence_plot 调用 matplotlib show() 函数,因此您可以继续自定义绘图 在最终调用“展示自己”之前``

关于python - 在 2x2 网格中绘制形状图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64521011/

相关文章:

matlab - 如何使用 tiledlayout 生成未对齐的子图?

python - 使用 cx_Freeze (PYTHON) 时导入模块错误

python - 使用 BPF 过滤器时 python 脚本的 CPU 使用率

python - 添加数字时处理 None

python - 如何将多个列绘制到单个seaborn箱线图中

python - 如何在 matplotlib 饼图中设置楔形边框?

python 2.7 : delete item from list by value

python - 如何让地 block 变大?

python - 使用函数和循环创建子图

python - ValueError : Invalid property specified for object of type plotly. graph_objs.Pie: 'xaxis'