From e8f95ab82aba754ab4db6db4c02c53c242e060e5 Mon Sep 17 00:00:00 2001 From: Jakub Nowak Date: Wed, 24 Jul 2019 17:56:38 +0200 Subject: [PATCH 1/4] Fix "The truth value of an array with more than one element is ambiguous" Fixes "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()" --- packages/python/plotly/plotly/subplots.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/python/plotly/plotly/subplots.py b/packages/python/plotly/plotly/subplots.py index 3a62a319fab..6af054941d4 100644 --- a/packages/python/plotly/plotly/subplots.py +++ b/packages/python/plotly/plotly/subplots.py @@ -511,13 +511,13 @@ def _checks(item, defaults): # ### vertical_spacing ### if vertical_spacing is None: - if subplot_titles: + if subplot_titles is not None: vertical_spacing = 0.5 / rows else: vertical_spacing = 0.3 / rows # ### subplot titles ### - if not subplot_titles: + if subplot_titles is None: subplot_titles = [""] * rows * cols # ### column_widths ### From 2c60cdb3bba0ed0f11b88e698f5a9315a0eb8df9 Mon Sep 17 00:00:00 2001 From: Jakub Nowak Date: Thu, 25 Jul 2019 16:19:41 +0200 Subject: [PATCH 2/4] Add tests --- .../test_subplots/test_make_subplots.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/packages/python/plotly/plotly/tests/test_core/test_subplots/test_make_subplots.py b/packages/python/plotly/plotly/tests/test_core/test_subplots/test_make_subplots.py index fa11f564941..2bc9eccb40b 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_subplots/test_make_subplots.py +++ b/packages/python/plotly/plotly/tests/test_core/test_subplots/test_make_subplots.py @@ -1,5 +1,7 @@ from __future__ import absolute_import +import numpy as np + from unittest import TestCase from plotly.graph_objs import ( Annotation, @@ -1542,6 +1544,41 @@ def test_subplot_titles_insets(self): ) self.assertEqual(fig, expected) + def test_subplot_titles_array(self): + # Pass python array + expected = tls.make_subplots( + insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], + subplot_titles=("", "Inset"), + ) + fig = tls.make_subplots( + insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], + subplot_titles=["", "Inset"], + ) + self.assertEqual(fig, expected) + + def test_subplot_titles_empty(self): + # Pass empty array + expected = tls.make_subplots( + insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], + ) + fig = tls.make_subplots( + insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], + subplot_titles=[], + ) + self.assertEqual(fig, expected) + + def test_subplot_titles_numpy_array(self): + # Pass numpy array + expected = tls.make_subplots( + insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], + subplot_titles=("", "Inset"), + ) + fig = tls.make_subplots( + insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], + subplot_titles=np.array(["", "Inset"]), + ) + self.assertEqual(fig, expected) + def test_large_columns_no_errors(self): """ Test that creating subplots with a large number of columns, and From 65da220957d3c6afefe6fcedec44baee2f8cd68e Mon Sep 17 00:00:00 2001 From: Jakub Nowak Date: Thu, 25 Jul 2019 20:53:35 +0200 Subject: [PATCH 3/4] Format code with black --- .../test_subplots/test_make_subplots.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/packages/python/plotly/plotly/tests/test_core/test_subplots/test_make_subplots.py b/packages/python/plotly/plotly/tests/test_core/test_subplots/test_make_subplots.py index 2bc9eccb40b..c13b72ec8ff 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_subplots/test_make_subplots.py +++ b/packages/python/plotly/plotly/tests/test_core/test_subplots/test_make_subplots.py @@ -1547,31 +1547,25 @@ def test_subplot_titles_insets(self): def test_subplot_titles_array(self): # Pass python array expected = tls.make_subplots( - insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], - subplot_titles=("", "Inset"), + insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], subplot_titles=("", "Inset") ) fig = tls.make_subplots( - insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], - subplot_titles=["", "Inset"], + insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], subplot_titles=["", "Inset"] ) self.assertEqual(fig, expected) def test_subplot_titles_empty(self): # Pass empty array - expected = tls.make_subplots( - insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], - ) + expected = tls.make_subplots(insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}]) fig = tls.make_subplots( - insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], - subplot_titles=[], + insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], subplot_titles=[] ) self.assertEqual(fig, expected) def test_subplot_titles_numpy_array(self): # Pass numpy array expected = tls.make_subplots( - insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], - subplot_titles=("", "Inset"), + insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], subplot_titles=("", "Inset") ) fig = tls.make_subplots( insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], From fe11de44030d3d9e0b601949f3b7b0657a4f070f Mon Sep 17 00:00:00 2001 From: Jakub Nowak Date: Sat, 27 Jul 2019 17:57:10 +0200 Subject: [PATCH 4/4] Move numpy test to test_optional --- .../test_subplots/test_make_subplots.py | 13 ------------- .../test_subplots/test_make_subplots.py | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 13 deletions(-) create mode 100644 packages/python/plotly/plotly/tests/test_optional/test_subplots/test_make_subplots.py diff --git a/packages/python/plotly/plotly/tests/test_core/test_subplots/test_make_subplots.py b/packages/python/plotly/plotly/tests/test_core/test_subplots/test_make_subplots.py index c13b72ec8ff..0b699859c47 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_subplots/test_make_subplots.py +++ b/packages/python/plotly/plotly/tests/test_core/test_subplots/test_make_subplots.py @@ -1,7 +1,5 @@ from __future__ import absolute_import -import numpy as np - from unittest import TestCase from plotly.graph_objs import ( Annotation, @@ -1562,17 +1560,6 @@ def test_subplot_titles_empty(self): ) self.assertEqual(fig, expected) - def test_subplot_titles_numpy_array(self): - # Pass numpy array - expected = tls.make_subplots( - insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], subplot_titles=("", "Inset") - ) - fig = tls.make_subplots( - insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], - subplot_titles=np.array(["", "Inset"]), - ) - self.assertEqual(fig, expected) - def test_large_columns_no_errors(self): """ Test that creating subplots with a large number of columns, and diff --git a/packages/python/plotly/plotly/tests/test_optional/test_subplots/test_make_subplots.py b/packages/python/plotly/plotly/tests/test_optional/test_subplots/test_make_subplots.py new file mode 100644 index 00000000000..0edcacebd1e --- /dev/null +++ b/packages/python/plotly/plotly/tests/test_optional/test_subplots/test_make_subplots.py @@ -0,0 +1,18 @@ +import numpy as np + +from unittest import TestCase + +import plotly.tools as tls + + +class TestMakeSubplots(TestCase): + def test_subplot_titles_numpy_array(self): + # Pass numpy array + expected = tls.make_subplots( + insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], subplot_titles=("", "Inset") + ) + fig = tls.make_subplots( + insets=[{"cell": (1, 1), "l": 0.7, "b": 0.3}], + subplot_titles=np.array(["", "Inset"]), + ) + self.assertEqual(fig, expected)