From 48b533d8e83ad8a540552339396db1c6bf6c4493 Mon Sep 17 00:00:00 2001 From: bensdm Date: Wed, 11 Nov 2020 19:20:56 +0100 Subject: [PATCH 1/5] fix annotations font color --- .../figure_factory/_annotated_heatmap.py | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/packages/python/plotly/plotly/figure_factory/_annotated_heatmap.py b/packages/python/plotly/plotly/figure_factory/_annotated_heatmap.py index 9b5bf0a0b17..6e19bfb4a9b 100644 --- a/packages/python/plotly/plotly/figure_factory/_annotated_heatmap.py +++ b/packages/python/plotly/plotly/figure_factory/_annotated_heatmap.py @@ -103,10 +103,9 @@ def create_annotated_heatmap( # validate colorscale colorscale_validator = ColorscaleValidator() colorscale = colorscale_validator.validate_coerce(colorscale) - annotations = _AnnotatedHeatmap( z, x, y, annotation_text, colorscale, font_colors, reversescale, **kwargs - ).make_annotations() + ).make_annotations(**kwargs) if x or y: trace = dict( @@ -211,7 +210,6 @@ def get_text_color(self): "Blues", "YIGnBu", "YIOrRd", - "RdBu", "Picnic", "Jet", "Hot", @@ -264,22 +262,21 @@ def get_text_color(self): max_text_color = black return min_text_color, max_text_color - def get_z_mid(self): + def get_z_min_max(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) + z_min = np.nanmin(self.z) + z_max = np.nanmax(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 + return z_min , z_max - def make_annotations(self): + def make_annotations(self, zmin=None, zmid=None, zmax=None): """ Get annotations for each cell of the heatmap with graph_objs.Annotation @@ -287,11 +284,20 @@ def make_annotations(self): the heatmap """ min_text_color, max_text_color = _AnnotatedHeatmap.get_text_color(self) - z_mid = _AnnotatedHeatmap.get_z_mid(self) + if zmin is None or zmax is None: + zmin, zmax = _AnnotatedHeatmap.get_z_min_max(self) + if zmid is None: + zmid = (zmax + zmin) / 2 + if min_text_color == max_text_color: + # diverging colorscale + mid_text_color = "#000000" + get_font_color = lambda val:mid_text_color if (zmin+zmid)/2 Date: Wed, 11 Nov 2020 19:49:02 +0100 Subject: [PATCH 2/5] black formatting --- packages/python/plotly/app.py | 14 ++++++ .../figure_factory/_annotated_heatmap.py | 44 +++++++++++++++---- 2 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 packages/python/plotly/app.py diff --git a/packages/python/plotly/app.py b/packages/python/plotly/app.py new file mode 100644 index 00000000000..5e07174eaa1 --- /dev/null +++ b/packages/python/plotly/app.py @@ -0,0 +1,14 @@ +# %% +from random import triangular +import plotly.figure_factory as ff + +z = [[round(triangular(0.01, 0.1), 3) for j in range(10)] for i in range(10)] +# ff.create_annotated_heatmap(z, colorscale='greens').show() +ff.create_annotated_heatmap(z, colorscale='RdBu', zmid=0).show() +# %% +ff.create_annotated_heatmap(z, colorscale='Greens', zmin=-0.3, zmax=0.1).show() + +# %% +ff.create_annotated_heatmap(z, colorscale='greens', zmin=0, zmax=1).show() + +# %% diff --git a/packages/python/plotly/plotly/figure_factory/_annotated_heatmap.py b/packages/python/plotly/plotly/figure_factory/_annotated_heatmap.py index 6e19bfb4a9b..21d09b66506 100644 --- a/packages/python/plotly/plotly/figure_factory/_annotated_heatmap.py +++ b/packages/python/plotly/plotly/figure_factory/_annotated_heatmap.py @@ -104,7 +104,14 @@ def create_annotated_heatmap( colorscale_validator = ColorscaleValidator() colorscale = colorscale_validator.validate_coerce(colorscale) annotations = _AnnotatedHeatmap( - z, x, y, annotation_text, colorscale, font_colors, reversescale, **kwargs + z, + x, + y, + annotation_text, + colorscale, + font_colors, + reversescale, + **kwargs ).make_annotations(**kwargs) if x or y: @@ -120,7 +127,9 @@ def create_annotated_heatmap( ) layout = dict( annotations=annotations, - xaxis=dict(ticks="", dtick=1, side="top", gridcolor="rgb(0, 0, 0)"), + xaxis=dict( + ticks="", dtick=1, side="top", gridcolor="rgb(0, 0, 0)" + ), yaxis=dict(ticks="", dtick=1, ticksuffix=" "), ) else: @@ -135,7 +144,10 @@ def create_annotated_heatmap( layout = dict( annotations=annotations, xaxis=dict( - ticks="", side="top", gridcolor="rgb(0, 0, 0)", showticklabels=False + ticks="", + side="top", + gridcolor="rgb(0, 0, 0)", + showticklabels=False, ), yaxis=dict(ticks="", ticksuffix=" ", showticklabels=False), ) @@ -168,7 +180,15 @@ class _AnnotatedHeatmap(object): """ def __init__( - self, z, x, y, annotation_text, colorscale, font_colors, reversescale, **kwargs + self, + z, + x, + y, + annotation_text, + colorscale, + font_colors, + reversescale, + **kwargs ): self.z = z @@ -242,7 +262,9 @@ def get_text_color(self): elif isinstance(self.colorscale, list): min_col = to_rgb_color_list(self.colorscale[0][1], [255, 255, 255]) - max_col = to_rgb_color_list(self.colorscale[-1][1], [255, 255, 255]) + max_col = to_rgb_color_list( + self.colorscale[-1][1], [255, 255, 255] + ) # swap min/max colors if reverse scale if self.reversescale: @@ -274,7 +296,7 @@ def get_z_min_max(self): 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]) - return z_min , z_max + return z_min, z_max def make_annotations(self, zmin=None, zmid=None, zmax=None): """ @@ -291,9 +313,15 @@ def make_annotations(self, zmin=None, zmid=None, zmax=None): if min_text_color == max_text_color: # diverging colorscale mid_text_color = "#000000" - get_font_color = lambda val:mid_text_color if (zmin+zmid)/2 Date: Wed, 11 Nov 2020 19:53:55 +0100 Subject: [PATCH 3/5] remove my test file --- packages/python/plotly/app.py | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 packages/python/plotly/app.py diff --git a/packages/python/plotly/app.py b/packages/python/plotly/app.py deleted file mode 100644 index 5e07174eaa1..00000000000 --- a/packages/python/plotly/app.py +++ /dev/null @@ -1,14 +0,0 @@ -# %% -from random import triangular -import plotly.figure_factory as ff - -z = [[round(triangular(0.01, 0.1), 3) for j in range(10)] for i in range(10)] -# ff.create_annotated_heatmap(z, colorscale='greens').show() -ff.create_annotated_heatmap(z, colorscale='RdBu', zmid=0).show() -# %% -ff.create_annotated_heatmap(z, colorscale='Greens', zmin=-0.3, zmax=0.1).show() - -# %% -ff.create_annotated_heatmap(z, colorscale='greens', zmin=0, zmax=1).show() - -# %% From 413eed601049e9ee1104ad241d224764030bbdfb Mon Sep 17 00:00:00 2001 From: bensdm Date: Wed, 11 Nov 2020 20:02:18 +0100 Subject: [PATCH 4/5] black reformatting --- .../figure_factory/_annotated_heatmap.py | 32 +++---------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/packages/python/plotly/plotly/figure_factory/_annotated_heatmap.py b/packages/python/plotly/plotly/figure_factory/_annotated_heatmap.py index 21d09b66506..fc47c783350 100644 --- a/packages/python/plotly/plotly/figure_factory/_annotated_heatmap.py +++ b/packages/python/plotly/plotly/figure_factory/_annotated_heatmap.py @@ -104,14 +104,7 @@ def create_annotated_heatmap( colorscale_validator = ColorscaleValidator() colorscale = colorscale_validator.validate_coerce(colorscale) annotations = _AnnotatedHeatmap( - z, - x, - y, - annotation_text, - colorscale, - font_colors, - reversescale, - **kwargs + z, x, y, annotation_text, colorscale, font_colors, reversescale, **kwargs ).make_annotations(**kwargs) if x or y: @@ -127,9 +120,7 @@ def create_annotated_heatmap( ) layout = dict( annotations=annotations, - xaxis=dict( - ticks="", dtick=1, side="top", gridcolor="rgb(0, 0, 0)" - ), + xaxis=dict(ticks="", dtick=1, side="top", gridcolor="rgb(0, 0, 0)"), yaxis=dict(ticks="", dtick=1, ticksuffix=" "), ) else: @@ -144,10 +135,7 @@ def create_annotated_heatmap( layout = dict( annotations=annotations, xaxis=dict( - ticks="", - side="top", - gridcolor="rgb(0, 0, 0)", - showticklabels=False, + ticks="", side="top", gridcolor="rgb(0, 0, 0)", showticklabels=False, ), yaxis=dict(ticks="", ticksuffix=" ", showticklabels=False), ) @@ -180,15 +168,7 @@ class _AnnotatedHeatmap(object): """ def __init__( - self, - z, - x, - y, - annotation_text, - colorscale, - font_colors, - reversescale, - **kwargs + self, z, x, y, annotation_text, colorscale, font_colors, reversescale, **kwargs ): self.z = z @@ -262,9 +242,7 @@ def get_text_color(self): elif isinstance(self.colorscale, list): min_col = to_rgb_color_list(self.colorscale[0][1], [255, 255, 255]) - max_col = to_rgb_color_list( - self.colorscale[-1][1], [255, 255, 255] - ) + max_col = to_rgb_color_list(self.colorscale[-1][1], [255, 255, 255]) # swap min/max colors if reverse scale if self.reversescale: From ae1f25f1574bf2c69fa346b47e8393a698e40d89 Mon Sep 17 00:00:00 2001 From: bensdm Date: Wed, 11 Nov 2020 20:25:31 +0100 Subject: [PATCH 5/5] update docstring --- .../python/plotly/plotly/figure_factory/_annotated_heatmap.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/python/plotly/plotly/figure_factory/_annotated_heatmap.py b/packages/python/plotly/plotly/figure_factory/_annotated_heatmap.py index fc47c783350..47e22b72240 100644 --- a/packages/python/plotly/plotly/figure_factory/_annotated_heatmap.py +++ b/packages/python/plotly/plotly/figure_factory/_annotated_heatmap.py @@ -264,9 +264,9 @@ def get_text_color(self): def get_z_min_max(self): """ - Get the mid value of z matrix + Get the min and max value of z matrix - :rtype (float) z_avg: average val from z matrix + :rtype (tuple): min and max val from z matrix """ if np and isinstance(self.z, np.ndarray): z_min = np.nanmin(self.z)