Skip to content

MNT DecisionBoundaryDisplay: change default colors and sampling - #33709

Merged
jeremiedbb merged 14 commits into
scikit-learn:mainfrom
AnneBeyer:dbd_change_default_colors_and_sampling
Apr 15, 2026
Merged

jeremiedbb merged 14 commits into
scikit-learn:mainfrom
AnneBeyer:dbd_change_default_colors_and_sampling

Conversation

@AnneBeyer

Copy link
Copy Markdown
Member

Reference Issues/PRs

Towards #33115, specifically the discussion around point 2.

What does this implement/fix? Explain your changes.

The default colors for the display (with <10 classes) are changed from tab10 to use the more accessible Petroff colors (following this paper and the matplotlib discussion here).

I also unified the sampling strategy for continuous and discreet colormaps, as this turned out to overcomplicate things by breaking consistency with how it is done by matplotlib.

I also adapted and unified the docstrings in all places where the color selection is mentioned and extended the test accordingly.

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

Any other comments?


multiclass_colors : list of color-like, str, or None
Color specification for multiclass plots.
multiclass_colors : str, or list of matplotlib colors, default=None

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I unified the docstrings for multiclass_colors based on the one in line 465 below. This now also includes a reference to matplotlib colors here (as requested by @lucyleeow here)

Returns
-------
colors : list or ndarray of shape (n_classes, 4)
colors : ndarray of shape (n_classes, 4)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

small fix of oversight from #33651

Comment thread sklearn/utils/fixes.py Resolved
@ogrisel

ogrisel commented Apr 14, 2026 •

Copy link
Copy Markdown
Member

For reference, here is the output of the usual plot script.

Details
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from sklearn.linear_model import LogisticRegression
from sklearn.inspection import DecisionBoundaryDisplay


data = np.array(
    [
        [-1, -1],
        [-2, -1],
        [1, 1],
        [2, 1],
        [2, 2],
        [3, 2],
        [3, 3],
    ]
)
target = np.arange(data.shape[0])
clf = LogisticRegression().fit(data, target)

_, axes = plt.subplots(nrows=3, ncols=3, figsize=(12, 12), constrained_layout=True)
for plot_method_idx, plot_method in enumerate(["contourf", "contour", "pcolormesh"]):
    for response_method_idx, response_method in enumerate(
        ["predict_proba", "decision_function", "predict"]
    ):
        ax = axes[plot_method_idx, response_method_idx]
        display = DecisionBoundaryDisplay.from_estimator(
            clf,
            data,
            response_method=response_method,
            plot_method=plot_method,
            ax=ax,
            alpha=0.5,
        )
        cmap = mpl.colors.ListedColormap(display.multiclass_colors_)
        ax.scatter(
            data[:, 0],
            data[:, 1],
            c=target.astype(int),
            edgecolors="black",
            cmap=cmap,
        )
        ax.set_title(f"plot_method={plot_method}\nresponse_method={response_method}")
image

BTW, we might want to upgrade the docstring of the class to do this plot instead. This would help users choose the combination of plot_method and response_method they want. (in a follow-up PR).

@ogrisel ogrisel left a comment

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.

LGTM once the conflict is resolved and CI is green again.

Co-authored-by: Jérémie du Boisberranger <[email protected]>
@github-actions github-actions Bot added CI:Linter failure The linter CI is failing on this PR and removed CI:Linter failure The linter CI is failing on this PR labels Apr 15, 2026
@AnneBeyer

Copy link
Copy Markdown
Member Author

Resolving the merge conflict in the GitHub editor broke the diff for sklearn/inspection/_plot/decision_boundary.py. I'm not sure how to resolve this.

@jeremiedbb

Copy link
Copy Markdown
Member

Resolving the merge conflict in the GitHub editor broke the diff for sklearn/inspection/_plot/decision_boundary.py. I'm not sure how to resolve this.

I would fetch this branch locally, revert to the state just before the merge, and do the merge again locally.

@AnneBeyer

Copy link
Copy Markdown
Member Author

I'm afraid I made it worse...

@AnneBeyer
AnneBeyer force-pushed the dbd_change_default_colors_and_sampling branch from f7bee49 to 018f03a Compare April 15, 2026 11:34
@AnneBeyer

Copy link
Copy Markdown
Member Author

Alright, I fixed the diff with a clean merge now.

Incorporating the contour change from main, this updates the plot from above to:
image
which I will add to the class example in a follow-up.

@jeremiedbb jeremiedbb left a comment

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.

LGTM. Thanks !

@jeremiedbb
jeremiedbb merged commit 54a6423 into scikit-learn:main Apr 15, 2026
33 checks passed
@github-project-automation github-project-automation Bot moved this from In progress to Done in Labs Apr 15, 2026
@AnneBeyer
AnneBeyer deleted the dbd_change_default_colors_and_sampling branch April 15, 2026 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

4 participants