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

Commit d7cf2de

Browse filesBrowse files
committed
Added tests to 2d_density
1 parent 8cf26dc commit d7cf2de
Copy full SHA for d7cf2de

File tree

Expand file treeCollapse file tree

2 files changed

+128
-3
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+128
-3
lines changed

‎plotly/tests/test_core/test_tools/test_figure_factory.py

Copy file name to clipboardExpand all lines: plotly/tests/test_core/test_tools/test_figure_factory.py
+110Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,116 @@ def test_gantt_all_args(self):
13651365
self.assertEqual(test_gantt_chart['layout'],
13661366
exp_gantt_chart['layout'])
13671367

1368+
1369+
class Test2D_Density(TestCase):
1370+
1371+
def test_validate_2D_density(self):
1372+
1373+
# validate that x and y contain only numbers
1374+
1375+
x = [1, 2]
1376+
1377+
y = ['a', 2]
1378+
1379+
pattern = ("All elements of your 'x' and 'y' lists must be numbers.")
1380+
1381+
self.assertRaisesRegexp(PlotlyError, pattern,
1382+
tls.FigureFactory.create_2D_density, x, y)
1383+
1384+
# validate that x and y are the same length
1385+
1386+
x2 = [1]
1387+
1388+
y2 = [1, 2]
1389+
1390+
pattern2 = ("Both lists 'x' and 'y' must be the same length.")
1391+
1392+
self.assertRaisesRegexp(PlotlyError, pattern2,
1393+
tls.FigureFactory.create_2D_density, x2, y2)
1394+
1395+
def test_2D_density_all_args(self):
1396+
1397+
# check if 2D_density data matches with expected output
1398+
1399+
x = [1, 2]
1400+
y = [2, 4]
1401+
1402+
colorscale = ['#7A4579', '#D56073', 'rgb(236,158,105)',
1403+
(1, 1, 0.2), (0.98, 0.98, 0.98)]
1404+
1405+
test_2D_density_chart = tls.FigureFactory.create_2D_density(
1406+
x, y, colorscale=colorscale, hist_color='rgb(255, 237, 222)',
1407+
point_size=3, height=800, width=800)
1408+
1409+
exp_2D_density_chart = {
1410+
'data': [{'marker': {'color': 'rgb(0.0, 0.0, 127.5)',
1411+
'opacity': 0.4,
1412+
'size': 3},
1413+
'mode': 'markers',
1414+
'name': 'points',
1415+
'type': 'scatter',
1416+
'x': [1, 2],
1417+
'y': [2, 4]},
1418+
{'colorscale': [[0.0, 'rgb(122.0, 69.0, 121.0)'],
1419+
[0.25, 'rgb(213.0, 96.0, 115.0)'],
1420+
[0.5, 'rgb(236.0, 158.0, 105.0)'],
1421+
[0.75, 'rgb(255.0, 255.0, 51.0)'],
1422+
[1.0, 'rgb(249.9, 249.9, 249.9)']],
1423+
'name': 'density',
1424+
'ncontours': 20,
1425+
'reversescale': True,
1426+
'showscale': False,
1427+
'type': 'histogram2dcontour',
1428+
'x': [1, 2],
1429+
'y': [2, 4]},
1430+
{'marker': {'color': 'rgb(255.0, 237.0, 222.0)'},
1431+
'name': 'x density',
1432+
'type': 'histogram',
1433+
'x': [1, 2],
1434+
'yaxis': 'y2'},
1435+
{'marker': {'color': 'rgb(255.0, 237.0, 222.0)'},
1436+
'name': 'y density',
1437+
'type': 'histogram',
1438+
'xaxis': 'x2',
1439+
'y': [2, 4]}],
1440+
'layout': {'autosize': False,
1441+
'bargap': 0,
1442+
'height': 800,
1443+
'hovermode': 'closest',
1444+
'margin': {'t': 50},
1445+
'showlegend': False,
1446+
'title': 'Love',
1447+
'width': 800,
1448+
'xaxis': {'domain': [0, 0.85],
1449+
'showgrid': False,
1450+
'zeroline': False},
1451+
'xaxis2': {'domain': [0.85, 1],
1452+
'showgrid': False,
1453+
'zeroline': False},
1454+
'yaxis': {'domain': [0, 0.85],
1455+
'showgrid': False,
1456+
'zeroline': False},
1457+
'yaxis2': {'domain': [0.85, 1],
1458+
'showgrid': False,
1459+
'zeroline': False}}
1460+
}
1461+
1462+
self.assertEqual(test_2D_density_chart['data'][0],
1463+
exp_2D_density_chart['data'][0])
1464+
1465+
self.assertEqual(test_2D_density_chart['data'][1],
1466+
exp_2D_density_chart['data'][1])
1467+
1468+
self.assertEqual(test_2D_density_chart['data'][2],
1469+
exp_2D_density_chart['data'][2])
1470+
1471+
self.assertEqual(test_2D_density_chart['data'][3],
1472+
exp_2D_density_chart['data'][3])
1473+
1474+
self.assertEqual(test_2D_density_chart['layout'],
1475+
exp_2D_density_chart['layout'])
1476+
1477+
13681478
# class TestDistplot(TestCase):
13691479

13701480
# def test_scipy_import_error(self):

‎plotly/tools.py

Copy file name to clipboardExpand all lines: plotly/tools.py
+18-3Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,8 +1494,8 @@ def create_2D_density(x, y, colorscale='Earth', ncontours=20,
14941494
"""
14951495
Returns figure for a 2D density plot
14961496
1497-
:param (list) x: x-axis data for plot generation
1498-
:param (list) y: y-axis data for plot generation
1497+
:param (list|array) x: x-axis data for plot generation
1498+
:param (list|array) y: y-axis data for plot generation
14991499
:param (str|tuple|list) colorscale: either a plotly scale name, an rgb
15001500
or hex color, a color tuple or a list or tuple of colors. An rgb
15011501
color is of the form 'rgb(x, y, z)' where x, y, z belong to the
@@ -1527,7 +1527,7 @@ def create_2D_density(x, y, colorscale='Earth', ncontours=20,
15271527
py.iplot(fig, filename='simple-2d-density')
15281528
```
15291529
1530-
Example 2: Use Parameters
1530+
Example 2: Using Parameters
15311531
```
15321532
import plotly.plotly as py
15331533
from plotly.tools import FigureFactory as FF
@@ -1551,6 +1551,21 @@ def create_2D_density(x, y, colorscale='Earth', ncontours=20,
15511551
```
15521552
"""
15531553
from plotly.graph_objs import graph_objs
1554+
from numbers import Number
1555+
1556+
# validate x and y are filled with numbers only
1557+
for array in [x, y]:
1558+
if not all(isinstance(element, Number) for element in array):
1559+
raise exceptions.PlotlyError(
1560+
"All elements of your 'x' and 'y' lists must be numbers."
1561+
)
1562+
1563+
# validate x and y are the same length
1564+
if len(x) != len(y):
1565+
raise exceptions.PlotlyError(
1566+
"Both lists 'x' and 'y' must be the same length."
1567+
)
1568+
15541569
colorscale = FigureFactory._validate_colors(colorscale, 'rgb')
15551570
colorscale = FigureFactory._make_linear_colorscale(colorscale)
15561571

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.