Skip to content

DOC fix DecisionBoundaryDisplay color consistency in plot_scaling_importance example#33654

Closed
yejiming wants to merge 1 commit intoscikit-learn:mainfrom
yejiming:codex/fix-scaling-importance-colors
Closed

DOC fix DecisionBoundaryDisplay color consistency in plot_scaling_importance example#33654
yejiming wants to merge 1 commit intoscikit-learn:mainfrom
yejiming:codex/fix-scaling-importance-colors

Conversation

@yejiming
Copy link
Copy Markdown

@yejiming yejiming commented Mar 28, 2026

Reference Issues/PRs

Towards #33557

What does this implement/fix? Explain your changes.

This PR makes the multiclass colors consistent between the
DecisionBoundaryDisplay background and the scatter plot in
examples/preprocessing/plot_scaling_importance.py.

It also cleans up a few small wording issues in the same example while keeping
the scope limited to this file.

I checked the rendered example locally with
EXAMPLES_PATTERN="plot_scaling_importance" make html
from the doc/ directory.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Copy link
Copy Markdown
Member

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I think we can simplify the code a bit though. The use of mpl.colors.ListedColormap does not seem to be necessary in particular.

Comment on lines +89 to +97
cmap = mpl.colors.ListedColormap(disp.multiclass_colors_)
disp.ax_.scatter(
X_plot["proline"],
X_plot["hue"],
c=y,
cmap=cmap,
s=20,
edgecolor="k",
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cmap = mpl.colors.ListedColormap(disp.multiclass_colors_)
disp.ax_.scatter(
X_plot["proline"],
X_plot["hue"],
c=y,
cmap=cmap,
s=20,
edgecolor="k",
)
disp.ax_.scatter(
X_plot["proline"], X_plot["hue"], c=y, cmap=cmap, s=20, edgecolor="k",
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of mpl.colors.ListedColormap will be needed again with the new approach.

X_plot,
response_method="predict",
alpha=0.5,
multiclass_colors=multiclass_cmap,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
multiclass_colors=multiclass_cmap,
multiclass_colors=cmap,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this could even be simplified to

Suggested change
multiclass_colors=multiclass_cmap,
multiclass_colors="viridis"

and the other two code changes could be removed, as viridis is the default for scatter anyways.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can then be removed to use the default colors.

X_plot = X[["proline", "hue"]]
X_plot_scaled = scaler.fit_transform(X_plot)
clf = KNeighborsClassifier(n_neighbors=20)
multiclass_cmap = "viridis"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
multiclass_cmap = "viridis"
cmap = "viridis"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will no longer be needed.

Copy link
Copy Markdown
Member

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that if we stick to the use of a continuous colormap such as "viridis", we don't need the extra complexity of the mpl.colors.ListedColormap.

We would only need this if we were to change this example to use a qualitative colormap. However as recently discussed in #33115 (comment), the default "tab10" qualitative color cycle is not colorblind-friendly (even for the first 3 colors), so unless we find a better qualitative color cycle for the 3 colors case, I am fine with using viridis for now.

# boundary that is much worse in comparison to a model trained on the full set
# of features.

import matplotlib as mpl
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import matplotlib as mpl

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be needed to enable cmap = mpl.colors.ListedColormap(disp.multiclass_colors_)

@AnneBeyer
Copy link
Copy Markdown
Contributor

Since we decided on a new default for colors in #33115, I'd suggest to wait for #33709 to be merged, and then we can use the new default for the decision boundary display and reuse it for the scatter plot.
@yejiming are you still interested in following up on this?

@AnneBeyer
Copy link
Copy Markdown
Contributor

@ogrisel, could you please close this one since there was no response from the author since it was opened three weeks ago? I'll fix this myself.

@ogrisel ogrisel closed this Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants