Skip to content

DOC make plot_agglomerative_clustering_metrics.py colorblind friendly #24655

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

Conversation

rprkh
Copy link
Contributor

@rprkh rprkh commented Oct 13, 2022

Reference Issues/PRs

Towards #5435
Related to #5435 (comment)

What does this implement/fix? Explain your changes.

Includes a more colorblind friendly palette. I think this is the last example that needs to be corrected for the issue to be closed. If theres any example remaining I'd be happy to make a PR to address that as well.

Any other comments?

Details (First Image)

Original Image (Deuteranopia)
image

Revised Image (Deutranopia)
image

Original Image (Protanopia)
image

Revised Image (Protanopia)
image

Original Image (Tritanopia)
image

Revised Image (Tritanopia)
image

@rprkh rprkh changed the title DOC make plot_agglomerative_clustering_metrics,py colorblind friendly DOC make plot_agglomerative_clustering_metrics.py colorblind friendly Oct 13, 2022
@ogrisel
Copy link
Member

ogrisel commented Oct 14, 2022

I tested the new plots with the "Let's go colorblind" browser extension and I confirm that this PR is a net improvement.

The black text labels are not readable in the cosine sim matrices when the background is dark (but this was already a problem on the main branch).

image

Maybe adding semi-opaque white bounding box under the text labels would help increase the constrast?

            t = plt.text(
                i,
                j,
                "%5.3f" % avg_dist[i, j],
                verticalalignment="center",
                horizontalalignment="center",
            )
            t.set_bbox(dict(facecolor='white', alpha=0.5, linewidth=0))

Credits: https://stackoverflow.com/questions/23696898/adjusting-text-background-transparency

@ogrisel
Copy link
Member

ogrisel commented Oct 14, 2022

Here is out it would look like (with the color palette on main but the idea would be the the same for this PR):

text_labels

Ideally I would rather have a thin semi-opaque white delineation around the black letters (like google maps uses to increase the contrast of text labels against the map background) instead of an ugly bbox I suggest above but I don't know how to do that with matplotlib.

Here is what I have in mind:

image

@ogrisel
Copy link
Member

ogrisel commented Oct 14, 2022

I found a way to do it!

diff --git a/examples/cluster/plot_agglomerative_clustering_metrics.py b/examples/cluster/plot_agglomerative_clustering_metrics.py
index 38fd3682d4..b60e6e84d5 100644
--- a/examples/cluster/plot_agglomerative_clustering_metrics.py
+++ b/examples/cluster/plot_agglomerative_clustering_metrics.py
@@ -38,6 +38,8 @@ thus the clustering puts them in the same cluster.
 # License: BSD 3-Clause or CC-0
 
 import matplotlib.pyplot as plt
+import matplotlib.patheffects as PathEffects
+
 import numpy as np
 
 from sklearn.cluster import AgglomerativeClustering
@@ -106,13 +108,14 @@ for index, metric in enumerate(["cosine", "euclidean", "cityblock"]):
     avg_dist /= avg_dist.max()
     for i in range(n_clusters):
         for j in range(n_clusters):
-            plt.text(
+            t = plt.text(
                 i,
                 j,
                 "%5.3f" % avg_dist[i, j],
                 verticalalignment="center",
                 horizontalalignment="center",
             )
+            t.set_path_effects([PathEffects.withStroke(linewidth=5, foreground='w', alpha=0.5)])
 
     plt.imshow(avg_dist, interpolation="nearest", cmap=plt.cm.gnuplot2, vmin=0)
     plt.xticks(range(n_clusters), labels, rotation=45)

Which gives:

text_labels

instead of:

text_labels_orig

Credits: https://osxastrotricks.wordpress.com/2014/12/02/add-border-around-text-with-matplotlib/

@rprkh
Copy link
Contributor Author

rprkh commented Oct 15, 2022

Included the suggested changes

Copy link
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.

Looks good to me! Thanks for the PR!

Copy link
Member

@adrinjalali adrinjalali left a comment

Choose a reason for hiding this comment

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

Nice improvement, thanks @rprkh

@adrinjalali adrinjalali merged commit 935f7e6 into scikit-learn:main Oct 17, 2022
@rprkh rprkh deleted the cb_friendly_agg_clustering_metrics branch October 17, 2022 13:48
glemaitre pushed a commit to glemaitre/scikit-learn that referenced this pull request Oct 31, 2022
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.

3 participants