Skip to content

Commit 2a22483

Browse files
committed
Merge pull request #275 from plotly/distplot
Distplot
2 parents 44c527e + 4702523 commit 2a22483

File tree

5 files changed

+573
-33
lines changed

5 files changed

+573
-33
lines changed

optional-requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ ipython[all]
1919

2020
## pandas deps for some matplotlib functionality ##
2121
pandas
22+
23+
## scipy deps for some FigureFactory functions ##
24+
scipy

plotly/tests/test_core/test_tools/test_figure_factory.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,3 +686,22 @@ def test_datetime_candlestick(self):
686686

687687
self.assertEqual(candle, exp_candle)
688688

689+
690+
# class TestDistplot(TestCase):
691+
692+
# def test_scipy_import_error(self):
693+
694+
# # make sure Import Error is raised when _scipy_imported = False
695+
696+
# hist_data = [[1.1, 1.1, 2.5, 3.0, 3.5,
697+
# 3.5, 4.1, 4.4, 4.5, 4.5,
698+
# 5.0, 5.0, 5.2, 5.5, 5.5,
699+
# 5.5, 5.5, 5.5, 6.1, 7.0]]
700+
701+
# group_labels = ['distplot example']
702+
703+
# self.assertRaisesRegexp(ImportError,
704+
# "FigureFactory.create_distplot requires scipy",
705+
# tls.FigureFactory.create_distplot,
706+
# hist_data, group_labels)
707+

plotly/tests/test_optional/test_opt_tracefactory.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,146 @@
99
import numpy as np
1010

1111

12+
class TestDistplot(TestCase):
13+
14+
def test_wrong_curve_type(self):
15+
16+
# check: PlotlyError (and specific message) is raised if curve_type is
17+
# not 'kde' or 'normal'
18+
19+
kwargs = {'hist_data': [[1, 2, 3]], 'group_labels': ['group'],
20+
'curve_type': 'curve'}
21+
self.assertRaisesRegexp(PlotlyError, "curve_type must be defined as "
22+
"'kde' or 'normal'",
23+
tls.FigureFactory.create_distplot, **kwargs)
24+
25+
def test_wrong_histdata_format(self):
26+
27+
# check: PlotlyError if hist_data is not a list of lists or list of
28+
# np.ndarrays (if hist_data is entered as just a list the function
29+
# will fail)
30+
31+
kwargs = {'hist_data': [1, 2, 3], 'group_labels': ['group']}
32+
self.assertRaises(PlotlyError, tls.FigureFactory.create_distplot,
33+
**kwargs)
34+
35+
def test_unequal_data_label_length(self):
36+
kwargs = {'hist_data': [[1, 2]], 'group_labels': ['group', 'group2']}
37+
self.assertRaises(PlotlyError, tls.FigureFactory.create_distplot,
38+
**kwargs)
39+
40+
kwargs = {'hist_data': [[1, 2], [1, 2, 3]], 'group_labels': ['group']}
41+
self.assertRaises(PlotlyError, tls.FigureFactory.create_distplot,
42+
**kwargs)
43+
44+
def test_simple_distplot(self):
45+
46+
# we should be able to create a single distplot with a simple dataset
47+
# and default kwargs
48+
49+
dp = tls.FigureFactory.create_distplot(hist_data=[[1, 2, 2, 3]],
50+
group_labels=['distplot'])
51+
expected_dp_layout = {'barmode': 'overlay',
52+
'hovermode': 'closest',
53+
'legend': {'traceorder': 'reversed'},
54+
'xaxis1': {'anchor': 'y2', 'domain': [0.0, 1.0], 'zeroline': False},
55+
'yaxis1': {'anchor': 'free', 'domain': [0.35, 1], 'position': 0.0},
56+
'yaxis2': {'anchor': 'x1',
57+
'domain': [0, 0.25],
58+
'dtick': 1,
59+
'showticklabels': False}}
60+
self.assertEqual(dp['layout'], expected_dp_layout)
61+
62+
expected_dp_data_hist = {'autobinx': False,
63+
'histnorm': 'probability',
64+
'legendgroup': 'distplot',
65+
'marker': {'color': 'rgb(31, 119, 180)'},
66+
'name': 'distplot',
67+
'opacity': 0.7,
68+
'type': 'histogram',
69+
'x': [1, 2, 2, 3],
70+
'xaxis': 'x1',
71+
'xbins': {'end': 3.0, 'size': 1.0, 'start': 1.0},
72+
'yaxis': 'y1'}
73+
self.assertEqual(dp['data'][0], expected_dp_data_hist)
74+
75+
expected_dp_data_rug = {'legendgroup': 'distplot',
76+
'marker': {'color': 'rgb(31, 119, 180)',
77+
'symbol': 'line-ns-open'},
78+
'mode': 'markers',
79+
'name': 'distplot',
80+
'showlegend': False,
81+
'text': None,
82+
'type': 'scatter',
83+
'x': [1, 2, 2, 3],
84+
'xaxis': 'x1',
85+
'y': ['distplot', 'distplot',
86+
'distplot', 'distplot'],
87+
'yaxis': 'y2'}
88+
self.assertEqual(dp['data'][2], expected_dp_data_rug)
89+
90+
def test_distplot_more_args(self):
91+
92+
# we should be able to create a distplot with 2 datasets no
93+
# rugplot, defined bin_size, and added title
94+
95+
hist1_x = [0.8, 1.2, 0.2, 0.6, 1.6,
96+
-0.9, -0.07, 1.95, 0.9, -0.2,
97+
-0.5, 0.3, 0.4, -0.37, 0.6]
98+
hist2_x = [0.8, 1.5, 1.5, 0.6, 0.59,
99+
1.0, 0.8, 1.7, 0.5, 0.8,
100+
-0.3, 1.2, 0.56, 0.3, 2.2]
101+
102+
hist_data = [hist1_x] + [hist2_x]
103+
group_labels = ['2012', '2013']
104+
105+
dp = tls.FigureFactory.create_distplot(hist_data, group_labels,
106+
show_rug=False, bin_size=.2)
107+
dp['layout'].update(title='Dist Plot')
108+
109+
expected_dp_layout = {'barmode': 'overlay',
110+
'hovermode': 'closest',
111+
'legend': {'traceorder': 'reversed'},
112+
'title': 'Dist Plot',
113+
'xaxis1': {'anchor': 'y2', 'domain': [0.0, 1.0],
114+
'zeroline': False},
115+
'yaxis1': {'anchor': 'free', 'domain': [0.0, 1],
116+
'position': 0.0}}
117+
self.assertEqual(dp['layout'], expected_dp_layout)
118+
119+
expected_dp_data_hist_1 = {'autobinx': False,
120+
'histnorm': 'probability',
121+
'legendgroup': '2012',
122+
'marker': {'color': 'rgb(31, 119, 180)'},
123+
'name': '2012',
124+
'opacity': 0.7,
125+
'type': 'histogram',
126+
'x': [0.8, 1.2, 0.2, 0.6, 1.6, -0.9, -0.07,
127+
1.95, 0.9, -0.2, -0.5, 0.3, 0.4,
128+
-0.37, 0.6],
129+
'xaxis': 'x1',
130+
'xbins': {'end': 1.95, 'size': 0.2,
131+
'start': -0.9},
132+
'yaxis': 'y1'}
133+
self.assertEqual(dp['data'][0], expected_dp_data_hist_1)
134+
135+
expected_dp_data_hist_2 = {'autobinx': False,
136+
'histnorm': 'probability',
137+
'legendgroup': '2013',
138+
'marker': {'color': 'rgb(255, 127, 14)'},
139+
'name': '2013',
140+
'opacity': 0.7,
141+
'type': 'histogram',
142+
'x': [0.8, 1.5, 1.5, 0.6, 0.59, 1.0, 0.8,
143+
1.7, 0.5, 0.8, -0.3, 1.2, 0.56, 0.3,
144+
2.2],
145+
'xaxis': 'x1',
146+
'xbins': {'end': 2.2, 'size': 0.2,
147+
'start': -0.3},
148+
'yaxis': 'y1'}
149+
self.assertEqual(dp['data'][1], expected_dp_data_hist_2)
150+
151+
12152
class TestStreamline(TestCase):
13153

14154
def test_wrong_arrow_scale(self):

0 commit comments

Comments
 (0)