Skip to content

Commit 4d68915

Browse files
committed
distplot
1 parent 295631e commit 4d68915

File tree

1 file changed

+65
-148
lines changed

1 file changed

+65
-148
lines changed

notebooks/distplot.md

Lines changed: 65 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
---
22
jupyter:
33
jupytext:
4+
notebook_metadata_filter: all
45
text_representation:
56
extension: .md
67
format_name: markdown
78
format_version: '1.1'
89
jupytext_version: 1.1.1
910
kernelspec:
10-
display_name: Python 2
11+
display_name: Python 3
1112
language: python
12-
name: python2
13+
name: python3
14+
language_info:
15+
codemirror_mode:
16+
name: ipython
17+
version: 3
18+
file_extension: .py
19+
mimetype: text/x-python
20+
name: python
21+
nbconvert_exporter: python
22+
pygments_lexer: ipython3
23+
version: 3.6.7
1324
plotly:
1425
description: How to make interactive Distplots in Python with Plotly.
1526
display_as: statistical
@@ -25,57 +36,36 @@ jupyter:
2536
title: Python Distplots | plotly
2637
---
2738

28-
<!-- #region {"deletable": true, "editable": true} -->
29-
#### New to Plotly?
30-
Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).
31-
<br>You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online).
32-
<br>We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
33-
<!-- #endregion -->
34-
35-
<!-- #region {"deletable": true, "editable": true} -->
36-
#### Version Check
37-
Note: Distplots are available in version <b>1.11.0+</b><br>
38-
Run `pip install plotly --upgrade` to update your Plotly version
39-
<!-- #endregion -->
40-
41-
```python deletable=true editable=true
42-
import plotly
43-
plotly.__version__
44-
```
39+
The distplot figure factory displays a combination of statistical representations of numerical data, such as histogram, kernel density estimation or normal curve, and rug plot.
40+
4541

46-
<!-- #region {"deletable": true, "editable": true} -->
4742
#### Basic Distplot
48-
<!-- #endregion -->
4943

50-
```python deletable=true editable=true
51-
import plotly.plotly as py
52-
import plotly.figure_factory as ff
44+
A histogram, a kde plot and a rug plot are displayed.
5345

46+
```python
47+
import plotly.figure_factory as ff
5448
import numpy as np
5549

5650
x = np.random.randn(1000)
5751
hist_data = [x]
58-
group_labels = ['distplot']
52+
group_labels = ['distplot'] # name of the dataset
5953

6054
fig = ff.create_distplot(hist_data, group_labels)
61-
py.iplot(fig, filename='Basic Distplot')
55+
fig.show()
6256
```
6357

64-
<!-- #region {"deletable": true, "editable": true} -->
6558
#### Plot Multiple Datasets
66-
<!-- #endregion -->
6759

68-
```python deletable=true editable=true
69-
import plotly.plotly as py
60+
```python
7061
import plotly.figure_factory as ff
71-
7262
import numpy as np
7363

7464
# Add histogram data
75-
x1 = np.random.randn(200)-2
65+
x1 = np.random.randn(200) - 2
7666
x2 = np.random.randn(200)
77-
x3 = np.random.randn(200)+2
78-
x4 = np.random.randn(200)+4
67+
x3 = np.random.randn(200) + 2
68+
x4 = np.random.randn(200) + 4
7969

8070
# Group data together
8171
hist_data = [x1, x2, x3, x4]
@@ -84,19 +74,15 @@ group_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4']
8474

8575
# Create distplot with custom bin_size
8676
fig = ff.create_distplot(hist_data, group_labels, bin_size=.2)
87-
88-
# Plot!
89-
py.iplot(fig, filename='Distplot with Multiple Datasets')
77+
fig.show()
9078
```
9179

92-
<!-- #region {"deletable": true, "editable": true} -->
9380
#### Use Multiple Bin Sizes
94-
<!-- #endregion -->
9581

96-
```python deletable=true editable=true
97-
import plotly.plotly as py
98-
import plotly.figure_factory as ff
82+
Different bin sizes are used for the different datasets with the `bin_size` argument.
9983

84+
```python
85+
import plotly.figure_factory as ff
10086
import numpy as np
10187

10288
# Add histogram data
@@ -112,91 +98,67 @@ group_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4']
11298

11399
# Create distplot with custom bin_size
114100
fig = ff.create_distplot(hist_data, group_labels, bin_size=[.1, .25, .5, 1])
115-
116-
# Plot!
117-
py.iplot(fig, filename='Distplot with Multiple Bin Sizes')
101+
fig.show()
118102
```
119103

120-
<!-- #region {"deletable": true, "editable": true} -->
121104
#### Customize Rug Text, Colors & Title
122-
<!-- #endregion -->
123105

124-
```python deletable=true editable=true
125-
import plotly.plotly as py
106+
```python
126107
import plotly.figure_factory as ff
127-
128108
import numpy as np
129109

130110
x1 = np.random.randn(26)
131111
x2 = np.random.randn(26) + .5
132112

133-
hist_data = [x1, x2]
134-
135113
group_labels = ['2014', '2015']
136114

137-
rug_text_one = ['a', 'b', 'c', 'd', 'e',
138-
'f', 'g', 'h', 'i', 'j',
139-
'k', 'l', 'm', 'n', 'o',
140-
'p', 'q', 'r', 's', 't',
115+
rug_text_one = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
116+
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
141117
'u', 'v', 'w', 'x', 'y', 'z']
142118

143-
rug_text_two = ['aa', 'bb', 'cc', 'dd', 'ee',
144-
'ff', 'gg', 'hh', 'ii', 'jj',
145-
'kk', 'll', 'mm', 'nn', 'oo',
146-
'pp', 'qq', 'rr', 'ss', 'tt',
119+
rug_text_two = ['aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg', 'hh', 'ii', 'jj',
120+
'kk', 'll', 'mm', 'nn', 'oo', 'pp', 'qq', 'rr', 'ss', 'tt',
147121
'uu', 'vv', 'ww', 'xx', 'yy', 'zz']
148122

149-
rug_text = [rug_text_one, rug_text_two]
150-
123+
rug_text = [rug_text_one, rug_text_two] # for hover in rug plot
151124
colors = ['rgb(0, 0, 100)', 'rgb(0, 200, 200)']
152125

153126
# Create distplot with custom bin_size
154127
fig = ff.create_distplot(
155-
hist_data, group_labels, bin_size=.2,
128+
[x1, x2], group_labels, bin_size=.2,
156129
rug_text=rug_text, colors=colors)
157130

158-
fig['layout'].update(title='Customized Distplot')
159-
160-
# Plot!
161-
py.iplot(fig, filename='Distplot Colors')
131+
fig.update(layout_title_text='Customized Distplot')
132+
fig.show()
162133
```
163134

164-
<!-- #region {"deletable": true, "editable": true} -->
165135
#### Plot Normal Curve
166-
<!-- #endregion -->
167136

168-
```python deletable=true editable=true
169-
import plotly.plotly as py
137+
```python
170138
import plotly.figure_factory as ff
171-
172139
import numpy as np
173140

174141
x1 = np.random.randn(200)
175142
x2 = np.random.randn(200) + 2
176-
hist_data = [x1, x2]
177143

178144
group_labels = ['Group 1', 'Group 2']
179145

180-
colors = ['#3A4750', '#F64E8B']
146+
colors = ['slategray', 'magenta']
181147

182148
# Create distplot with curve_type set to 'normal'
183-
fig = ff.create_distplot(hist_data, group_labels, bin_size=.5, curve_type='normal', colors=colors)
149+
fig = ff.create_distplot([x1, x2], group_labels, bin_size=.5,
150+
curve_type='normal', # override default 'kde'
151+
colors=colors)
184152

185153
# Add title
186-
fig['layout'].update(title='Distplot with Normal Distribution')
187-
188-
# Plot!
189-
py.iplot(fig, filename='Distplot with Normal Curve')
154+
fig.update(layout_title_text='Distplot with Normal Distribution')
155+
fig.show()
190156
```
191157

192-
<!-- #region {"deletable": true, "editable": true} -->
193158
#### Plot Only Curve and Rug
194-
<!-- #endregion -->
195159

196-
```python deletable=true editable=true
197-
import plotly.plotly as py
160+
```python
198161
import plotly.figure_factory as ff
199-
200162
import numpy as np
201163

202164
x1 = np.random.randn(200) - 1
@@ -212,20 +174,14 @@ colors = ['#333F44', '#37AA9C', '#94F3E4']
212174
fig = ff.create_distplot(hist_data, group_labels, show_hist=False, colors=colors)
213175

214176
# Add title
215-
fig['layout'].update(title='Curve and Rug Plot')
216-
217-
# Plot!
218-
py.iplot(fig, filename='Curve and Rug')
177+
fig.update(layout_title_text='Curve and Rug Plot')
178+
fig.show()
219179
```
220180

221-
<!-- #region {"deletable": true, "editable": true} -->
222181
#### Plot Only Hist and Rug
223-
<!-- #endregion -->
224182

225-
```python deletable=true editable=true
226-
import plotly.plotly as py
183+
```python
227184
import plotly.figure_factory as ff
228-
229185
import numpy as np
230186

231187
x1 = np.random.randn(200) - 1
@@ -238,23 +194,18 @@ group_labels = ['Group 1', 'Group 2', 'Group 3']
238194
colors = ['#835AF1', '#7FA6EE', '#B8F7D4']
239195

240196
# Create distplot with curve_type set to 'normal'
241-
fig = ff.create_distplot(hist_data, group_labels, colors=colors, bin_size=.25, show_curve=False)
197+
fig = ff.create_distplot(hist_data, group_labels, colors=colors, bin_size=.25,
198+
show_curve=False)
242199

243200
# Add title
244-
fig['layout'].update(title='Hist and Rug Plot')
245-
246-
# Plot!
247-
py.iplot(fig, filename='Hist and Rug')
201+
fig.update(layout_title_text='Hist and Rug Plot')
202+
fig.show()
248203
```
249204

250-
<!-- #region {"deletable": true, "editable": true} -->
251205
#### Plot Hist and Rug with Different Bin Sizes
252-
<!-- #endregion -->
253206

254-
```python deletable=true editable=true
255-
import plotly.plotly as py
207+
```python
256208
import plotly.figure_factory as ff
257-
258209
import numpy as np
259210

260211
x1 = np.random.randn(200) - 2
@@ -265,25 +216,19 @@ hist_data = [x1, x2, x3]
265216

266217
group_labels = ['Group 1', 'Group 2', 'Group 3']
267218
colors = ['#393E46', '#2BCDC1', '#F66095']
268-
# Create distplot with curve_type set to 'normal'
219+
269220
fig = ff.create_distplot(hist_data, group_labels, colors=colors,
270221
bin_size=[0.3, 0.2, 0.1], show_curve=False)
271222

272223
# Add title
273-
fig['layout'].update(title='Hist and Rug Plot')
274-
275-
# Plot!
276-
py.iplot(fig, filename='Hist and Rug Different Bin Size')
224+
fig.update(layout_title_text='Hist and Rug Plot')
225+
fig.show()
277226
```
278227

279-
<!-- #region {"deletable": true, "editable": true} -->
280228
#### Plot Only Hist and Curve
281-
<!-- #endregion -->
282229

283-
```python deletable=true editable=true
284-
import plotly.plotly as py
230+
```python
285231
import plotly.figure_factory as ff
286-
287232
import numpy as np
288233

289234
x1 = np.random.randn(200) - 2
@@ -300,56 +245,28 @@ fig = ff.create_distplot(hist_data, group_labels, colors=colors,
300245
bin_size=.2, show_rug=False)
301246

302247
# Add title
303-
fig['layout'].update(title='Hist and Curve Plot')
304-
305-
# Plot!
306-
py.iplot(fig, filename='Hist and Curve')
248+
fig.update(layout_title_text='Hist and Curve Plot')
249+
fig.show()
307250
```
308251

309-
<!-- #region {"deletable": true, "editable": true} -->
310252
#### Distplot with Pandas
311-
<!-- #endregion -->
312253

313-
```python deletable=true editable=true
314-
import plotly.plotly as py
254+
```python
315255
import plotly.figure_factory as ff
316-
317256
import numpy as np
318257
import pandas as pd
319258

320259
df = pd.DataFrame({'2012': np.random.randn(200),
321260
'2013': np.random.randn(200)+1})
322-
py.iplot(ff.create_distplot([df[c] for c in df.columns], df.columns, bin_size=.25),
323-
filename='distplot with pandas')
261+
fig = ff.create_distplot([df[c] for c in df.columns], df.columns, bin_size=.25)
262+
fig.show()
324263
```
325264

326-
<!-- #region {"deletable": true, "editable": true} -->
327265
#### Reference
328-
<!-- #endregion -->
329266

330-
```python deletable=true editable=true
267+
```python
331268
help(ff.create_distplot)
332269
```
270+
```python
333271

334-
```python deletable=true editable=true
335-
from IPython.display import display, HTML
336-
337-
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
338-
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
339-
340-
!pip install git+https://github.com/plotly/publisher.git --upgrade
341-
import publisher
342-
publisher.publish(
343-
'distplots.ipynb', 'python/distplot/', 'Python Distplots | plotly',
344-
'How to make interactive Distplots in Python with Plotly. ',
345-
title = 'Python Distplots | plotly',
346-
name = 'Distplots',
347-
has_thumbnail='true', thumbnail='thumbnail/distplot.jpg',
348-
language='python', page_type='example_index',
349-
display_as='statistical', order=5,
350-
ipynb= '~notebook_demo/23')
351272
```
352-
353-
```python deletable=true editable=true
354-
355-
```

0 commit comments

Comments
 (0)