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

Skip to content

Commit d11dcde

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

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

examples/ensemble/plot_gradient_boosting_regression.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
#
2525
# License: BSD 3 clause
2626

27+
import matplotlib
2728
import matplotlib.pyplot as plt
2829
import numpy as np
2930

3031
from sklearn import datasets, ensemble
3132
from sklearn.inspection import permutation_importance
3233
from sklearn.metrics import mean_squared_error
3334
from sklearn.model_selection import train_test_split
35+
from sklearn.utils.fixes import parse_version
3436

3537
# %%
3638
# Load the data
@@ -148,11 +150,20 @@
148150
)
149151
sorted_idx = result.importances_mean.argsort()
150152
plt.subplot(1, 2, 2)
151-
plt.boxplot(
152-
result.importances[sorted_idx].T,
153-
vert=False,
154-
labels=np.array(diabetes.feature_names)[sorted_idx],
153+
154+
# `labels` argument in boxplot is deprecated in matplotlib 3.9 and has been
155+
# renamed to `tick_labels`. The following code handles this, but as a
156+
# scikit-learn user you probably can write simpler code by using `labels=...`
157+
# (matplotlib < 3.9) or `tick_labels=...` (matplotlib >= 3.9).
158+
tick_labels_parameter_name = (
159+
"tick_labels"
160+
if parse_version(matplotlib.__version__) >= parse_version("3.9")
161+
else "labels"
155162
)
163+
tick_labels_dict = {
164+
tick_labels_parameter_name: np.array(diabetes.feature_names)[sorted_idx]
165+
}
166+
plt.boxplot(result.importances[sorted_idx].T, vert=False, **tick_labels_dict)
156167
plt.title("Permutation Importance (test set)")
157168
fig.tight_layout()
158169
plt.show()

examples/inspection/plot_permutation_importance_multicollinear.py

Lines changed: 13 additions & 4 deletions
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+
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

Lines changed: 13 additions & 2 deletions
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` a 6D4E rgument 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