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 cf48f62

Browse filesBrowse files
author
Kevin Yan
committed
Merge pull request #473 from plotly/distplot-fix
Distplot fix
2 parents dfef7af + bda2531 commit cf48f62
Copy full SHA for cf48f62

File tree

Expand file treeCollapse file tree

2 files changed

+194
-23
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+194
-23
lines changed

‎plotly/tests/test_optional/test_figure_factory.py

Copy file name to clipboardExpand all lines: plotly/tests/test_optional/test_figure_factory.py
+175-14Lines changed: 175 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,60 @@ def test_unequal_data_label_length(self):
4444
self.assertRaises(PlotlyError, tls.FigureFactory.create_distplot,
4545
**kwargs)
4646

47-
def test_simple_distplot(self):
47+
def test_simple_distplot_prob_density(self):
4848

4949
# we should be able to create a single distplot with a simple dataset
5050
# and default kwargs
5151

5252
dp = tls.FigureFactory.create_distplot(hist_data=[[1, 2, 2, 3]],
53-
group_labels=['distplot'])
53+
group_labels=['distplot'],
54+
histnorm='probability density')
55+
expected_dp_layout = {'barmode': 'overlay',
56+
'hovermode': 'closest',
57+
'legend': {'traceorder': 'reversed'},
58+
'xaxis1': {'anchor': 'y2', 'domain': [0.0, 1.0], 'zeroline': False},
59+
'yaxis1': {'anchor': 'free', 'domain': [0.35, 1], 'position': 0.0},
60+
'yaxis2': {'anchor': 'x1',
61+
'domain': [0, 0.25],
62+
'dtick': 1,
63+
'showticklabels': False}}
64+
self.assertEqual(dp['layout'], expected_dp_layout)
65+
66+
expected_dp_data_hist = {'autobinx': False,
67+
'histnorm': 'probability density',
68+
'legendgroup': 'distplot',
69+
'marker': {'color': 'rgb(31, 119, 180)'},
70+
'name': 'distplot',
71+
'opacity': 0.7,
72+
'type': 'histogram',
73+
'x': [1, 2, 2, 3],
74+
'xaxis': 'x1',
75+
'xbins': {'end': 3.0, 'size': 1.0, 'start': 1.0},
76+
'yaxis': 'y1'}
77+
self.assertEqual(dp['data'][0], expected_dp_data_hist)
78+
79+
expected_dp_data_rug = {'legendgroup': 'distplot',
80+
'marker': {'color': 'rgb(31, 119, 180)',
81+
'symbol': 'line-ns-open'},
82+
'mode': 'markers',
83+
'name': 'distplot',
84+
'showlegend': False,
85+
'text': None,
86+
'type': 'scatter',
87+
'x': [1, 2, 2, 3],
88+
'xaxis': 'x1',
89+
'y': ['distplot', 'distplot',
90+
'distplot', 'distplot'],
91+
'yaxis': 'y2'}
92+
self.assertEqual(dp['data'][2], expected_dp_data_rug)
93+
94+
def test_simple_distplot_prob(self):
95+
96+
# we should be able to create a single distplot with a simple dataset
97+
# and default kwargs
98+
99+
dp = tls.FigureFactory.create_distplot(hist_data=[[1, 2, 2, 3]],
100+
group_labels=['distplot'], histnorm='probability')
54101
expected_dp_layout = {'barmode': 'overlay',
55102
'hovermode': 'closest',
56103
'legend': {'traceorder': 'reversed'},
@@ -90,7 +137,7 @@ def test_simple_distplot(self):
90137
'yaxis': 'y2'}
91138
self.assertEqual(dp['data'][2], expected_dp_data_rug)
92139

93-
def test_distplot_more_args(self):
140+
def test_distplot_more_args_prob_dens(self):
94141

95142
# we should be able to create a distplot with 2 datasets no
96143
# rugplot, defined bin_size, and added title
@@ -106,6 +153,69 @@ def test_distplot_more_args(self):
106153
group_labels = ['2012', '2013']
107154

108155
dp = tls.FigureFactory.create_distplot(hist_data, group_labels,
156+
histnorm='probability density',
157+
show_rug=False, bin_size=.2)
158+
dp['layout'].update(title='Dist Plot')
159+
160+
expected_dp_layout = {'barmode': 'overlay',
161+
'hovermode': 'closest',
162+
'legend': {'traceorder': 'reversed'},
163+
'title': 'Dist Plot',
164+
'xaxis1': {'anchor': 'y2', 'domain': [0.0, 1.0],
165+
'zeroline': False},
166+
'yaxis1': {'anchor': 'free', 'domain': [0.0, 1],
167+
'position': 0.0}}
168+
self.assertEqual(dp['layout'], expected_dp_layout)
169+
170+
expected_dp_data_hist_1 = {'autobinx': False,
171+
'histnorm': 'probability density',
172+
'legendgroup': '2012',
173+
'marker': {'color': 'rgb(31, 119, 180)'},
174+
'name': '2012',
175+
'opacity': 0.7,
176+
'type': 'histogram',
177+
'x': [0.8, 1.2, 0.2, 0.6, 1.6, -0.9, -0.07,
178+
1.95, 0.9, -0.2, -0.5, 0.3, 0.4,
179+
-0.37, 0.6],
180+
'xaxis': 'x1',
181+
'xbins': {'end': 1.95, 'size': 0.2,
182+
'start': -0.9},
183+
'yaxis': 'y1'}
184+
self.assertEqual(dp['data'][0], expected_dp_data_hist_1)
185+
186+
expected_dp_data_hist_2 = {'autobinx': False,
187+
'histnorm': 'probability density',
188+
'legendgroup': '2013',
189+
'marker': {'color': 'rgb(255, 127, 14)'},
190+
'name': '2013',
191+
'opacity': 0.7,
192+
'type': 'histogram',
193+
'x': [0.8, 1.5, 1.5, 0.6, 0.59, 1.0, 0.8,
194+
1.7, 0.5, 0.8, -0.3, 1.2, 0.56, 0.3,
195+
2.2],
196+
'xaxis': 'x1',
197+
'xbins': {'end': 2.2, 'size': 0.2,
198+
'start': -0.3},
199+
'yaxis': 'y1'}
200+
self.assertEqual(dp['data'][1], expected_dp_data_hist_2)
201+
202+
def test_distplot_more_args_prob(self):
203+
204+
# we should be able to create a distplot with 2 datasets no
205+
# rugplot, defined bin_size, and added title
206+
207+
hist1_x = [0.8, 1.2, 0.2, 0.6, 1.6,
208+
-0.9, -0.07, 1.95, 0.9, -0.2,
209+
-0.5, 0.3, 0.4, -0.37, 0.6]
210+
hist2_x = [0.8, 1.5, 1.5, 0.6, 0.59,
211+
1.0, 0.8, 1.7, 0.5, 0.8,
212+
-0.3, 1.2, 0.56, 0.3, 2.2]
213+
214+
hist_data = [hist1_x] + [hist2_x]
215+
group_labels = ['2012', '2013']
216+
217+
dp = tls.FigureFactory.create_distplot(hist_data, group_labels,
218+
histnorm='probability',
109219
show_rug=False, bin_size=.2)
110220
dp['layout'].update(title='Dist Plot')
111221

@@ -151,7 +261,55 @@ def test_distplot_more_args(self):
151261
'yaxis': 'y1'}
152262
self.assertEqual(dp['data'][1], expected_dp_data_hist_2)
153263

154-
def test_distplot_binsize_array(self):
264+
def test_distplot_binsize_array_prob(self):
265+
hist1_x = [0.8, 1.2, 0.2, 0.6, 1.6, -0.9, -0.07, 1.95, 0.9, -0.2,
266+
-0.5, 0.3, 0.4, -0.37, 0.6]
267+
hist2_x = [0.8, 1.5, 1.5, 0.6, 0.59, 1.0, 0.8, 1.7, 0.5, 0.8, -0.3,
268+
1.2, 0.56, 0.3, 2.2]
269+
270+
hist_data = [hist1_x, hist2_x]
271+
group_labels = ['2012', '2013']
272+
273+
dp = tls.FigureFactory.create_distplot(hist_data, group_labels,
274+
histnorm='probability',
275+
show_rug=False,
276+
bin_size=[.2, .2])
277+
278+
expected_dp_data_hist_1 = {'autobinx': False,
279+
'histnorm': 'probability density',
280+
'legendgroup': '2012',
281+
'marker':
282+
{'color': 'rgb(31, 119, 180)'},
283+
'name': '2012',
284+
'opacity': 0.7,
285+
'type': 'histogram',
286+
'x': [0.8, 1.2, 0.2, 0.6, 1.6, -0.9,
287+
-0.07, 1.95, 0.9, -0.2, -0.5, 0.3,
288+
0.4, -0.37, 0.6],
289+
'xaxis': 'x1',
290+
'xbins': {'end': 1.95, 'size': 0.2,
291+
'start': -0.9},
292+
'yaxis': 'y1'}
293+
self.assertEqual(dp['data'][0], expected_dp_data_hist_1)
294+
295+
expected_dp_data_hist_2 = {'autobinx': False,
296+
'histnorm': 'probability density',
297+
'legendgroup': '2013',
298+
'marker':
299+
{'color': 'rgb(255, 127, 14)'},
300+
'name': '2013',
301+
'opacity': 0.7,
302+
'type': 'histogram',
303+
'x': [0.8, 1.5, 1.5, 0.6, 0.59, 1.0,
304+
0.8, 1.7, 0.5, 0.8, -0.3, 1.2,
305+
0.56, 0.3, 2.2],
306+
'xaxis': 'x1',
307+
'xbins': {'end': 2.2, 'size': 0.2,
308+
'start': -0.3},
309+
'yaxis': 'y1'}
310+
self.assertEqual(dp['data'][1], expected_dp_data_hist_2)
311+
312+
def test_distplot_binsize_array_prob_density(self):
155313
hist1_x = [0.8, 1.2, 0.2, 0.6, 1.6, -0.9, -0.07, 1.95, 0.9, -0.2,
156314
-0.5, 0.3, 0.4, -0.37, 0.6]
157315
hist2_x = [0.8, 1.5, 1.5, 0.6, 0.59, 1.0, 0.8, 1.7, 0.5, 0.8, -0.3,
@@ -161,35 +319,38 @@ def test_distplot_binsize_array(self):
161319
group_labels = ['2012', '2013']
162320

163321
dp = tls.FigureFactory.create_distplot(hist_data, group_labels,
322+
histnorm='probability',
164323
show_rug=False,
165324
bin_size=[.2, .2])
166325

167326
expected_dp_data_hist_1 = {'autobinx': False,
168-
'histnorm': 'probability',
327+
'histnorm': 'probability density',
169328
'legendgroup': '2012',
170-
'marker': {'color': 'rgb(31, 119, 180)'},
329+
'marker':
330+
{'color': 'rgb(31, 119, 180)'},
171331
'name': '2012',
172332
'opacity': 0.7,
173333
'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],
334+
'x': [0.8, 1.2, 0.2, 0.6, 1.6, -0.9,
335+
-0.07, 1.95, 0.9, -0.2, -0.5, 0.3,
336+
0.4, -0.37, 0.6],
177337
'xaxis': 'x1',
178338
'xbins': {'end': 1.95, 'size': 0.2,
179339
'start': -0.9},
180340
'yaxis': 'y1'}
181341
self.assertEqual(dp['data'][0], expected_dp_data_hist_1)
182342

183343
expected_dp_data_hist_2 = {'autobinx': False,
184-
'histnorm': 'probability',
344+
'histnorm': 'probability density',
185345
'legendgroup': '2013',
186-
'marker': {'color': 'rgb(255, 127, 14)'},
346+
'marker':
347+
{'color': 'rgb(255, 127, 14)'},
187348
'name': '2013',
188349
'opacity': 0.7,
189350
'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],
351+
'x': [0.8, 1.5, 1.5, 0.6, 0.59, 1.0,
352+
0.8, 1.7, 0.5, 0.8, -0.3, 1.2,
353+
0.56, 0.3, 2.2],
193354
'xaxis': 'x1',
194355
'xbins': {'end': 2.2, 'size': 0.2,
195356
'start': -0.3},

‎plotly/tools.py

Copy file name to clipboardExpand all lines: plotly/tools.py
+19-9Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
'rgb(227, 119, 194)', 'rgb(127, 127, 127)',
2929
'rgb(188, 189, 34)', 'rgb(23, 190, 207)']
3030

31+
DEFAULT_HISTNORM = 'probability density'
32+
ALTERNATIVE_HISTNORM = 'probability'
33+
3134

3235
# Warning format
3336
def warning_on_one_line(message, category, filename, lineno,
@@ -4245,7 +4248,7 @@ def create_candlestick(open, high, low, close,
42454248
@staticmethod
42464249
def create_distplot(hist_data, group_labels,
42474250
bin_size=1., curve_type='kde',
4248-
colors=[], rug_text=[],
4251+
colors=[], rug_text=[], histnorm=DEFAULT_HISTNORM,
42494252
show_hist=True, show_curve=True,
42504253
show_rug=True):
42514254
"""
@@ -4262,6 +4265,8 @@ def create_distplot(hist_data, group_labels,
42624265
:param (list[float]|float) bin_size: Size of histogram bins.
42634266
Default = 1.
42644267
:param (str) curve_type: 'kde' or 'normal'. Default = 'kde'
4268+
:param (str) histnorm: 'probability density' or 'probability'
4269+
Default = 'probability density'
42654270
:param (bool) show_hist: Add histogram to distplot? Default = True
42664271
:param (bool) show_curve: Add curve to distplot? Default = True
42674272
:param (bool) show_rug: Add rug to distplot? Default = True
@@ -4370,23 +4375,23 @@ def create_distplot(hist_data, group_labels,
43704375
bin_size = [bin_size]*len(hist_data)
43714376

43724377
hist = _Distplot(
4373-
hist_data, group_labels, bin_size,
4378+
hist_data, histnorm, group_labels, bin_size,
43744379
curve_type, colors, rug_text,
43754380
show_hist, show_curve).make_hist()
43764381

43774382
if curve_type == 'normal':
43784383
curve = _Distplot(
4379-
hist_data, group_labels, bin_size,
4384+
hist_data, histnorm, group_labels, bin_size,
43804385
curve_type, colors, rug_text,
43814386
show_hist, show_curve).make_normal()
43824387
else:
43834388
curve = _Distplot(
4384-
hist_data, group_labels, bin_size,
4389+
hist_data, histnorm, group_labels, bin_size,
43854390
curve_type, colors, rug_text,
43864391
show_hist, show_curve).make_kde()
43874392

43884393
rug = _Distplot(
4389-
hist_data, group_labels, bin_size,
4394+
hist_data, histnorm, group_labels, bin_size,
43904395
curve_type, colors, rug_text,
43914396
show_hist, show_curve).make_rug()
43924397

@@ -5262,10 +5267,11 @@ class _Distplot(FigureFactory):
52625267
"""
52635268
Refer to TraceFactory.create_distplot() for docstring
52645269
"""
5265-
def __init__(self, hist_data, group_labels,
5270+
def __init__(self, hist_data, histnorm, group_labels,
52665271
bin_size, curve_type, colors,
52675272
rug_text, show_hist, show_curve):
52685273
self.hist_data = hist_data
5274+
self.histnorm = histnorm
52695275
self.group_labels = group_labels
52705276
self.bin_size = bin_size
52715277
self.show_hist = show_hist
@@ -5307,7 +5313,7 @@ def make_hist(self):
53075313
x=self.hist_data[index],
53085314
xaxis='x1',
53095315
yaxis='y1',
5310-
histnorm='probability',
5316+
histnorm=self.histnorm,
53115317
name=self.group_labels[index],
53125318
legendgroup=self.group_labels[index],
53135319
marker=dict(color=self.colors[index]),
@@ -5334,7 +5340,9 @@ def make_kde(self):
53345340
self.curve_y[index] = (scipy.stats.gaussian_kde
53355341
(self.hist_data[index])
53365342
(self.curve_x[index]))
5337-
self.curve_y[index] *= self.bin_size[index]
5343+
5344+
if self.histnorm == ALTERNATIVE_HISTNORM:
5345+
self.curve_y[index] *= self.bin_size[index]
53385346

53395347
for index in range(self.trace_number):
53405348
curve[index] = dict(type='scatter',
@@ -5369,7 +5377,9 @@ def make_normal(self):
53695377
/ 500 for x in range(500)]
53705378
self.curve_y[index] = scipy.stats.norm.pdf(
53715379
self.curve_x[index], loc=mean[index], scale=sd[index])
5372-
self.curve_y[index] *= self.bin_size[index]
5380+
5381+
if self.histnorm == ALTERNATIVE_HISTNORM:
5382+
self.curve_y[index] *= self.bin_size[index]
53735383

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

0 commit comments

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