8000 EXA Use tick_labels in boxplot for matplotlib>=3.9 (#29471) · scikit-learn/scikit-learn@13a1791 · GitHub
[go: up one dir, main page]

Skip to content

Commit 13a1791

Browse files
nithish08lesteve
andauthored
EXA Use tick_labels in boxplot for matplotlib>=3.9 (#29471)
Co-authored-by: Loïc Estève <loic.esteve@ymail.com>
1 parent dc6c01c commit 13a1791

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

examples/ensemble/plot_gradient_boosting_regression.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
# Authors: The scikit-learn developers
2222
# SPDX-License-Identifier: BSD-3-Clause
2323

24+
import matplotlib
2425
import matplotlib.pyplot as plt
2526
import numpy as np
2627

2728
from sklearn import datasets, ensemble
2829
from sklearn.inspection import permutation_importance
2930
from sklearn.metrics import mean_squared_error
3031
from sklearn.model_selection import train_test_split
32+
from sklearn.utils.fixes import parse_version
3133

3234
# %%
3335
# Load the data
@@ -145,11 +147,20 @@
145147
)
146148
sorted_idx = result.importances_mean.argsort()
147149
plt.subplot(1, 2, 2)
148-
plt.boxplot(
149-
result.importances[sorted_idx].T,
150-
vert=False,
151-
labels=np.array(diabetes.feature_names)[sorted_idx],
150+
151+
# `labels` argument in boxplot is deprecated in matplotlib 3.9 and has been
152+
# renamed to `tick_labels`. The following code handles this, but as a
153+
# scikit-learn user you probably can write simpler code by using `labels=...`
154+
# (matplotlib < 3.9) or `tick_labels=...` (matplotlib >= 3.9).
155+
tick_labels_parameter_name = (
156+
"tick_labels"
157+
if parse_version(matplotlib.__version__) >= parse_version("3.9")
158+
else "labels"
152159
)
160+
tick_labels_dict = {
161+
tick_labels_parameter_name: np.array(diabetes.feature_names)[sorted_idx]
162+
}
163+
plt.boxplot(result.importances[sorted_idx].T, vert=False, **tick_labels_dict)
153164
plt.title("Permutation Importance (test set)")
154165
fig.tight_layout()
155166
plt.show()

examples/inspection/plot_permutation_importance_multicollinear.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,27 @@
2626
# ------------------------------------------------------
2727
#
2828
# First, we define a function to ease the plotting:
29+
< 10000 span class=pl-k>import matplotlib
30+
2931
from sklearn.inspection import permutation_importance
32+
from sklearn.utils.fixes import parse_version
3033

3134

3235
def plot_permutation_importance(clf, X, y, ax):
3336
result = permutation_importance(clf, X, y, n_repeats=10, random_state=42, n_jobs=2)
3437
perm_sorted_idx = result.importances_mean.argsort()
3538

36-
ax.boxplot(
37-
result.importances[perm_sorted_idx].T,
38-
vert=False,
39-
labels=X.columns[perm_sorted_idx],
39+
# `labels` argument in boxplot is deprecated in matplotlib 3.9 and has been
40+
# renamed to `tick_labels`. The following code handles this, but as a
41+
# scikit-learn user you probably can write simpler code by using `labels=...`
42+
# (matplotlib < 3.9) or `tick_labels=...` (matplotlib >= 3.9).
43+
tick_labels_parameter_name = (
44+
"tick_labels"
45+
if parse_version(matplotlib.__version__) >= parse_version("3.9")
46+
else "labels"
4047
)
48+
tick_labels_dict = {tick_labels_parameter_name: X.columns[perm_sorted_idx]}
49+
ax.boxplot(result.importances[perm_sorted_idx].T, vert=False, **tick_labels_dict)
4150
ax.axvline(x=0, color="k", linestyle="--")
4251
return ax
4352

examples/release_highlights/plot_release_highlights_0_22_0.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
# `plot_confusion_matrix`. Read more about this new API in the
3535
# :ref:`User Guide <visualizations>`.
3636

37+
import matplotlib
3738
import matplotlib.pyplot as plt
3839

3940
from sklearn.datasets import make_classification
@@ -43,6 +44,7 @@
4344
from sklearn.metrics import RocCurveDisplay
4445
from sklearn.model_selection import train_test_split
4546
from sklearn.svm import SVC
47+
from sklearn.utils.fixes import parse_version
4648

4749
X, y = make_classification(random_state=0)
4850
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
@@ -117,9 +119,18 @@
117119

118120
fig, ax = plt.subplots()
119121
sorted_idx = result.importances_mean.argsort()
120-
ax.boxplot(
121-
result.importances[sorted_idx].T, vert=False, labels=feature_names[sorted_idx]
122+
123+
# `labels` argument in boxplot is deprecated in matplotlib 3.9 and has been
124+
# renamed to `tick_labels`. The following code handles this, but as a
125+
# scikit-learn user you probably can write simpler code by using `labels=...`
126+
# (matplotlib < 3.9) or `tick_labels=...` (matplotlib >= 3.9).
127+
tick_labels_parameter_name = (
128+
"tick_labels"
129+
if parse_version(matplotlib.__version__) >= parse_version("3.9")
130+
else "labels"
122131
)
132+
tick_labels_dict = {tick_labels_parameter_name: feature_names[sorted_idx]}
133+
ax.boxplot(result.importances[sorted_idx].T, vert=False, **tick_labels_dict)
123134
ax.set_title("Permutation Importance of each feature")
124135
ax.set_ylabel("Features")
125136
fig.tight_layout()

0 commit comments

Comments
 (0)
0