-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
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
DOC make plot_agglomerative_clustering_metrics.py
colorblind friendly
#24655
Conversation
plot_agglomerative_clustering_metrics,py
colorblind friendlyplot_agglomerative_clustering_metrics.py
colorblind friendly
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 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 |
Here is out it would look like (with the color palette on 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 Here is what I have in mind: |
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: instead of: Credits: https://osxastrotricks.wordpress.com/2014/12/02/add-border-around-text-with-matplotlib/ |
Included the suggested changes |
There was a problem hiding this 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!
There was a problem hiding this 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
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)

Revised Image (Deutranopia)

Original Image (Protanopia)

Revised Image (Protanopia)

Original Image (Tritanopia)

Revised Image (Tritanopia)
