Skip to content

Commit 0a57c11

Browse files
author
Kevin Yan
committed
Merge pull request #455 from plotly/distplot_update
added the option for multiple binsizes
2 parents 524e4fc + 7a8ef34 commit 0a57c11

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

plotly/tests/test_optional/test_figure_factory.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,51 @@ def test_distplot_more_args(self):
151151
'yaxis': 'y1'}
152152
self.assertEqual(dp['data'][1], expected_dp_data_hist_2)
153153

154+
def test_distplot_binsize_array(self):
155+
hist1_x = [0.8, 1.2, 0.2, 0.6, 1.6, -0.9, -0.07, 1.95, 0.9, -0.2,
156+
-0.5, 0.3, 0.4, -0.37, 0.6]
157+
hist2_x = [0.8, 1.5, 1.5, 0.6, 0.59, 1.0, 0.8, 1.7, 0.5, 0.8, -0.3,
158+
1.2, 0.56, 0.3, 2.2]
159+
160+
hist_data = [hist1_x, hist2_x]
161+
group_labels = ['2012', '2013']
162+
163+
dp = tls.FigureFactory.create_distplot(hist_data, group_labels,
164+
show_rug=False,
165+
bin_size=[.2, .2])
166+
167+
expected_dp_data_hist_1 = {'autobinx': False,
168+
'histnorm': 'probability',
169+
'legendgroup': '2012',
170+
'marker': {'color': 'rgb(31, 119, 180)'},
171+
'name': '2012',
172+
'opacity': 0.7,
173+
'type': 'histogram',
174+
'x': [0.8, 1.2, 0.2, 0.6, 1.6, -0.9, -0.07,
175+
1.95, 0.9, -0.2, -0.5, 0.3, 0.4,
176+
-0.37, 0.6],
177+
'xaxis': 'x1',
178+
'xbins': {'end': 1.95, 'size': 0.2,
179+
'start': -0.9},
180+
'yaxis': 'y1'}
181+
self.assertEqual(dp['data'][0], expected_dp_data_hist_1)
182+
183+
expected_dp_data_hist_2 = {'autobinx': False,
184+
'histnorm': 'probability',
185+
'legendgroup': '2013',
186+
'marker': {'color': 'rgb(255, 127, 14)'},
187+
'name': '2013',
188+
'opacity': 0.7,
189+
'type': 'histogram',
190+
'x': [0.8, 1.5, 1.5, 0.6, 0.59, 1.0, 0.8,
191+
1.7, 0.5, 0.8, -0.3, 1.2, 0.56, 0.3,
192+
2.2],
193+
'xaxis': 'x1',
194+
'xbins': {'end': 2.2, 'size': 0.2,
195+
'start': -0.3},
196+
'yaxis': 'y1'}
197+
self.assertEqual(dp['data'][1], expected_dp_data_hist_2)
198+
154199

155200
class TestStreamline(TestCase):
156201

@@ -424,7 +469,7 @@ def test_dendrogram_random_matrix(self):
424469
self.assert_dict_equal(dendro['layout'], expected_dendro['layout'])
425470

426471
def test_dendrogram_orientation(self):
427-
X = np.random.rand(5, 5)
472+
X = np.random.rand(5, 5)
428473

429474
dendro_left = tls.FigureFactory.create_dendrogram(
430475
X, orientation='left')

plotly/tools.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4033,7 +4033,8 @@ def create_distplot(hist_data, group_labels,
40334033
:param (list[list]) hist_data: Use list of lists to plot multiple data
40344034
sets on the same plot.
40354035
:param (list[str]) group_labels: Names for each data set.
4036-
:param (float) bin_size: Size of histogram bins. Default = 1.
4036+
:param (list[float]|float) bin_size: Size of histogram bins.
4037+
Default = 1.
40374038
:param (str) curve_type: 'kde' or 'normal'. Default = 'kde'
40384039
:param (bool) show_hist: Add histogram to distplot? Default = True
40394040
:param (bool) show_curve: Add curve to distplot? Default = True
@@ -4139,6 +4140,9 @@ def create_distplot(hist_data, group_labels,
41394140
FigureFactory._validate_distplot(hist_data, curve_type)
41404141
FigureFactory._validate_equal_length(hist_data, group_labels)
41414142

4143+
if isinstance(bin_size, (float, int)):
4144+
bin_size = [bin_size]*len(hist_data)
4145+
41424146
hist = _Distplot(
41434147
hist_data, group_labels, bin_size,
41444148
curve_type, colors, rug_text,
@@ -5084,7 +5088,7 @@ def make_hist(self):
50845088
autobinx=False,
50855089
xbins=dict(start=self.start[index],
50865090
end=self.end[index],
5087-
size=self.bin_size),
5091+
size=self.bin_size[index]),
50885092
opacity=.7)
50895093
return hist
50905094

@@ -5104,7 +5108,7 @@ def make_kde(self):
51045108
self.curve_y[index] = (scipy.stats.gaussian_kde
51055109
(self.hist_data[index])
51065110
(self.curve_x[index]))
5107-
self.curve_y[index] *= self.bin_size
5111+
self.curve_y[index] *= self.bin_size[index]
51085112

51095113
for index in range(self.trace_number):
51105114
curve[index] = dict(type='scatter',
@@ -5139,7 +5143,7 @@ def make_normal(self):
51395143
/ 500 for x in range(500)]
51405144
self.curve_y[index] = scipy.stats.norm.pdf(
51415145
self.curve_x[index], loc=mean[index], scale=sd[index])
5142-
self.curve_y[index] *= self.bin_size
5146+
self.curve_y[index] *= self.bin_size[index]
51435147

51445148
for index in range(self.trace_number):
51455149
curve[index] = dict(type='scatter',

0 commit comments

Comments
 (0)