Skip to content

Commit 38ce18c

Browse files
Add options to customize the color bar of the violin plot (#4119)
* Add label font size options for color bar * Remove unused arguments * Update docstrings for violin plot * Update font size for color bar * Bring back unused arguments * Recover the previous behavior --------- Co-authored-by: Tobias Pitters <[email protected]>
1 parent d66b56e commit 38ce18c

1 file changed

Lines changed: 41 additions & 23 deletions

File tree

‎shap/plots/_violin.py‎

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,44 +41,62 @@ def violin(
4141
class_inds=None,
4242
color_bar_label=labels["FEATURE_VALUE"],
4343
cmap=colors.red_blue,
44+
color_bar_label_size=12,
45+
color_bar_tick_size=11,
46+
axhline_lw=0.5,
4447
use_log_scale=False,
4548
):
4649
"""Create a SHAP violin plot, colored by feature values when they are provided.
4750
4851
Parameters
4952
----------
50-
shap_values : Explanation, or numpy.array
53+
shap_values : Explanation or numpy.ndarray
5154
For single output explanations, this is a matrix of SHAP values (# samples x # features).
52-
53-
features : numpy.array or pandas.DataFrame or list
54-
Matrix of feature values (# samples x # features) or a ``feature_names`` list as
55-
shorthand.
56-
57-
feature_names : list
55+
features : numpy.ndarray or pandas.DataFrame or list, optional
56+
Matrix of feature values (# samples x # features), or a ``feature_names`` list as shorthand.
57+
feature_names : list, optional
5858
Names of the features (length: # features).
59-
60-
max_display : int
59+
max_display : int, optional
6160
How many top features to include in the plot (default is 20).
62-
63-
plot_type : "violin", or "layered_violin".
61+
plot_type : {"violin", "layered_violin"}, optional
6462
What type of summary plot to produce. A "layered_violin" plot shows the
6563
distribution of the SHAP values of each variable. A "violin" plot is the same,
6664
except with outliers drawn as scatter points.
67-
68-
color_bar : bool
69-
Whether to draw the color bar (legend).
70-
71-
show : bool
65+
color : str or None, optional
66+
Color or colormap to use for the plot. If None, a default is chosen.
67+
axis_color : str, optional
68+
Color for the plot axes.
69+
title : str or None, optional
70+
Plot title (currently unused).
71+
alpha : float, optional
72+
Opacity of the plot elements.
73+
show : bool, optional
7274
Whether :external+mpl:func:`matplotlib.pyplot.show()` is called before returning.
73-
Setting this to ``False`` allows the plot
74-
to be customized further after it has been created.
75-
76-
plot_size : "auto" (default), float, (float, float), or None
75+
Setting this to ``False`` allows the plot to be customized further after it has been created.
76+
sort : bool, optional
77+
Whether to sort features by the sum of their effect magnitudes.
78+
color_bar : bool, optional
79+
Whether to draw the color bar (legend).
80+
plot_size : {"auto", float, (float, float), None}, optional
7781
What size to make the plot. By default, the size is auto-scaled based on the number of
7882
features that are being displayed. Passing a single float will cause each row to be that
7983
many inches high. Passing a pair of floats will scale the plot by that
8084
number of inches. If ``None`` is passed, then the size of the current figure will be left
8185
unchanged.
86+
layered_violin_max_num_bins : int, optional
87+
Maximum number of bins for layered violin plots.
88+
color_bar_label : str, optional
89+
Label for the color bar.
90+
cmap : str or Colormap, optional
91+
Colormap to use for coloring points by feature value.
92+
color_bar_label_size : int, optional
93+
Font size for the color bar label. Default is 11.
94+
color_bar_tick_size : int, optional
95+
Font size for the color bar ticks. Default is 11.
96+
axhline_lw : float, optional
97+
Line width for horizontal lines in the plot.
98+
use_log_scale : bool, optional
99+
Whether to use a symmetric log scale for the x-axis.
82100
83101
Examples
84102
--------
@@ -170,7 +188,7 @@ def violin(
170188

171189
if plot_type == "violin":
172190
for pos in range(len(feature_order)):
173-
plt.axhline(y=pos, color="#cccccc", lw=0.5, dashes=(1, 5), zorder=-1)
191+
plt.axhline(y=pos, color="#cccccc", lw=axhline_lw, dashes=(1, 5), zorder=-1)
174192

175193
if features is not None:
176194
global_low = np.nanpercentile(shap_values[:, : len(feature_names)].flatten(), 1)
@@ -346,8 +364,8 @@ def violin(
346364
m.set_array([0, 1])
347365
cb = plt.colorbar(m, ax=plt.gca(), ticks=[0, 1], aspect=80)
348366
cb.set_ticklabels([labels["FEATURE_VALUE_LOW"], labels["FEATURE_VALUE_HIGH"]])
349-
cb.set_label(color_bar_label, size=12, labelpad=0)
350-
cb.ax.tick_params(labelsize=11, length=0)
367+
cb.set_label(color_bar_label, size=color_bar_label_size, labelpad=0)
368+
cb.ax.tick_params(labelsize=color_bar_tick_size, length=0)
351369
cb.set_alpha(1)
352370
cb.outline.set_visible(False) # type: ignore
353371
# bbox = cb.ax.get_window_extent().transformed(plt.gcf().dpi_scale_trans.inverted())

0 commit comments

Comments
 (0)