Skip to content

FIX Uses the color max for colormap in ConfusionMatrixDisplay #19784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whats_new/v1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,9 @@ Changelog
are integral.
:pr:`9843` by :user:`Jon Crall <Erotemic>`.

- |Fix| :meth:`metrics.ConfusionMatrixDisplay.plot` uses the correct max
for colormap. :pr:`19784` by `Thomas Fan`_.

- |Fix| Samples with zero `sample_weight` values do not affect the results
from :func:`metrics.det_curve`, :func:`metrics.precision_recall_curve`
and :func:`metrics.roc_curve`.
Expand Down
2 changes: 1 addition & 1 deletion sklearn/metrics/_plot/confusion_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def plot(self, *, include_values=True, cmap='viridis',
n_classes = cm.shape[0]
self.im_ = ax.imshow(cm, interpolation='nearest', cmap=cmap)
self.text_ = None
cmap_min, cmap_max = self.im_.cmap(0), self.im_.cmap(256)
cmap_min, cmap_max = self.im_.cmap(0), self.im_.cmap(1.0)

if include_values:
self.text_ = np.empty_like(cm, dtype=object)
Expand Down
14 changes: 14 additions & 0 deletions sklearn/metrics/_plot/tests/test_confusion_matrix_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,17 @@ def test_confusion_matrix_with_unknown_labels(pyplot, constructor_name):
display_labels = [tick.get_text() for tick in disp.ax_.get_xticklabels()]
expected_labels = [str(i) for i in range(n_classes + 1)]
assert_array_equal(expected_labels, display_labels)


def test_colormap_max(pyplot):
"""Check that the max color is used for the color of the text."""

from matplotlib import cm
gray = cm.get_cmap('gray', 1024)
confusion_matrix = np.array([[1.0, 0.0], [0.0, 1.0]])

disp = ConfusionMatrixDisplay(confusion_matrix)
disp.plot(cmap=gray)

color = disp.text_[1, 0].get_color()
assert_allclose(color, [1.0, 1.0, 1.0, 1.0])