Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

honor zmin/zmid/zmax in annotated_heatmap font colors #2921

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 3 commits into from
Nov 26, 2020
Merged
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
35 changes: 18 additions & 17 deletions 35 packages/python/plotly/plotly/figure_factory/_annotated_heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,23 @@ def __init__(
self.reversescale = reversescale
self.font_colors = font_colors

if np and isinstance(self.z, np.ndarray):
self.zmin = np.amin(self.z)
self.zmax = np.amax(self.z)
Copy link

@bensdm bensdm Nov 27, 2020

Choose a reason for hiding this comment

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

I think we should use np.nanmin and np.nanmax

else:
self.zmin = min([v for row in self.z for v in row])
self.zmax = max([v for row in self.z for v in row])

if kwargs.get("zmin", None) is not None:
self.zmin = kwargs["zmin"]
if kwargs.get("zmax", None) is not None:
self.zmax = kwargs["zmax"]

self.zmid = (self.zmax + self.zmin) / 2

if kwargs.get("zmid", None) is not None:
self.zmid = kwargs["zmid"]

def get_text_color(self):
"""
Get font color for annotations.
Expand Down Expand Up @@ -264,21 +281,6 @@ def get_text_color(self):
max_text_color = black
return min_text_color, max_text_color

def get_z_mid(self):
"""
Get the mid value of z matrix

:rtype (float) z_avg: average val from z matrix
"""
if np and isinstance(self.z, np.ndarray):
z_min = np.amin(self.z)
z_max = np.amax(self.z)
else:
z_min = min([v for row in self.z for v in row])
z_max = max([v for row in self.z for v in row])
z_mid = (z_max + z_min) / 2
return z_mid

def make_annotations(self):
"""
Get annotations for each cell of the heatmap with graph_objs.Annotation
Expand All @@ -287,11 +289,10 @@ def make_annotations(self):
the heatmap
"""
min_text_color, max_text_color = _AnnotatedHeatmap.get_text_color(self)
z_mid = _AnnotatedHeatmap.get_z_mid(self)
annotations = []
for n, row in enumerate(self.z):
for m, val in enumerate(row):
font_color = min_text_color if val < z_mid else max_text_color
font_color = min_text_color if val < self.zmid else max_text_color
Copy link

Choose a reason for hiding this comment

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

what about diverging colorscale?

annotations.append(
graph_objs.layout.Annotation(
text=str(self.annotation_text[n][m]),
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.