1
1
---
2
2
jupyter :
3
3
jupytext :
4
+ notebook_metadata_filter : all
4
5
text_representation :
5
6
extension : .md
6
7
format_name : markdown
7
8
format_version : ' 1.1'
8
9
jupytext_version : 1.1.1
9
10
kernelspec :
10
- display_name : Python 2
11
+ display_name : Python 3
11
12
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
13
24
plotly :
14
25
description : How to make interactive Distplots in Python with Plotly.
15
26
display_as : statistical
@@ -25,57 +36,36 @@ jupyter:
25
36
title : Python Distplots | plotly
26
37
---
27
38
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
+
45
41
46
- <!-- #region {"deletable": true, "editable": true} -->
47
42
#### Basic Distplot
48
- <!-- #endregion -->
49
43
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.
53
45
46
+ ``` python
47
+ import plotly.figure_factory as ff
54
48
import numpy as np
55
49
56
50
x = np.random.randn(1000 )
57
51
hist_data = [x]
58
- group_labels = [' distplot' ]
52
+ group_labels = [' distplot' ] # name of the dataset
59
53
60
54
fig = ff.create_distplot(hist_data, group_labels)
61
- py.iplot(fig, filename = ' Basic Distplot ' )
55
+ fig.show( )
62
56
```
63
57
64
- <!-- #region {"deletable": true, "editable": true} -->
65
58
#### Plot Multiple Datasets
66
- <!-- #endregion -->
67
59
68
- ``` python deletable=true editable=true
69
- import plotly.plotly as py
60
+ ``` python
70
61
import plotly.figure_factory as ff
71
-
72
62
import numpy as np
73
63
74
64
# Add histogram data
75
- x1 = np.random.randn(200 )- 2
65
+ x1 = np.random.randn(200 ) - 2
76
66
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
79
69
80
70
# Group data together
81
71
hist_data = [x1, x2, x3, x4]
@@ -84,19 +74,15 @@ group_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4']
84
74
85
75
# Create distplot with custom bin_size
86
76
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()
90
78
```
91
79
92
- <!-- #region {"deletable": true, "editable": true} -->
93
80
#### Use Multiple Bin Sizes
94
- <!-- #endregion -->
95
81
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.
99
83
84
+ ``` python
85
+ import plotly.figure_factory as ff
100
86
import numpy as np
101
87
102
88
# Add histogram data
@@ -112,91 +98,67 @@ group_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4']
112
98
113
99
# Create distplot with custom bin_size
114
100
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()
118
102
```
119
103
120
- <!-- #region {"deletable": true, "editable": true} -->
121
104
#### Customize Rug Text, Colors & Title
122
- <!-- #endregion -->
123
105
124
- ``` python deletable=true editable=true
125
- import plotly.plotly as py
106
+ ``` python
126
107
import plotly.figure_factory as ff
127
-
128
108
import numpy as np
129
109
130
110
x1 = np.random.randn(26 )
131
111
x2 = np.random.randn(26 ) + .5
132
112
133
- hist_data = [x1, x2]
134
-
135
113
group_labels = [' 2014' , ' 2015' ]
136
114
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' ,
141
117
' u' , ' v' , ' w' , ' x' , ' y' , ' z' ]
142
118
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' ,
147
121
' uu' , ' vv' , ' ww' , ' xx' , ' yy' , ' zz' ]
148
122
149
- rug_text = [rug_text_one, rug_text_two]
150
-
123
+ rug_text = [rug_text_one, rug_text_two] # for hover in rug plot
151
124
colors = [' rgb(0, 0, 100)' , ' rgb(0, 200, 200)' ]
152
125
153
126
# Create distplot with custom bin_size
154
127
fig = ff.create_distplot(
155
- hist_data , group_labels, bin_size = .2 ,
128
+ [x1, x2] , group_labels, bin_size = .2 ,
156
129
rug_text = rug_text, colors = colors)
157
130
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()
162
133
```
163
134
164
- <!-- #region {"deletable": true, "editable": true} -->
165
135
#### Plot Normal Curve
166
- <!-- #endregion -->
167
136
168
- ``` python deletable=true editable=true
169
- import plotly.plotly as py
137
+ ``` python
170
138
import plotly.figure_factory as ff
171
-
172
139
import numpy as np
173
140
174
141
x1 = np.random.randn(200 )
175
142
x2 = np.random.randn(200 ) + 2
176
- hist_data = [x1, x2]
177
143
178
144
group_labels = [' Group 1' , ' Group 2' ]
179
145
180
- colors = [' #3A4750 ' , ' #F64E8B ' ]
146
+ colors = [' slategray ' , ' magenta ' ]
181
147
182
148
# 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)
184
152
185
153
# 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()
190
156
```
191
157
192
- <!-- #region {"deletable": true, "editable": true} -->
193
158
#### Plot Only Curve and Rug
194
- <!-- #endregion -->
195
159
196
- ``` python deletable=true editable=true
197
- import plotly.plotly as py
160
+ ``` python
198
161
import plotly.figure_factory as ff
199
-
200
162
import numpy as np
201
163
202
164
x1 = np.random.randn(200 ) - 1
@@ -212,20 +174,14 @@ colors = ['#333F44', '#37AA9C', '#94F3E4']
212
174
fig = ff.create_distplot(hist_data, group_labels, show_hist = False , colors = colors)
213
175
214
176
# 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()
219
179
```
220
180
221
- <!-- #region {"deletable": true, "editable": true} -->
222
181
#### Plot Only Hist and Rug
223
- <!-- #endregion -->
224
182
225
- ``` python deletable=true editable=true
226
- import plotly.plotly as py
183
+ ``` python
227
184
import plotly.figure_factory as ff
228
-
229
185
import numpy as np
230
186
231
187
x1 = np.random.randn(200 ) - 1
@@ -238,23 +194,18 @@ group_labels = ['Group 1', 'Group 2', 'Group 3']
238
194
colors = [' #835AF1' , ' #7FA6EE' , ' #B8F7D4' ]
239
195
240
196
# 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 )
242
199
243
200
# 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()
248
203
```
249
204
250
- <!-- #region {"deletable": true, "editable": true} -->
251
205
#### Plot Hist and Rug with Different Bin Sizes
252
- <!-- #endregion -->
253
206
254
- ``` python deletable=true editable=true
255
- import plotly.plotly as py
207
+ ``` python
256
208
import plotly.figure_factory as ff
257
-
258
209
import numpy as np
259
210
260
211
x1 = np.random.randn(200 ) - 2
@@ -265,25 +216,19 @@ hist_data = [x1, x2, x3]
265
216
266
217
group_labels = [' Group 1' , ' Group 2' , ' Group 3' ]
267
218
colors = [' #393E46' , ' #2BCDC1' , ' #F66095' ]
268
- # Create distplot with curve_type set to 'normal'
219
+
269
220
fig = ff.create_distplot(hist_data, group_labels, colors = colors,
270
221
bin_size = [0.3 , 0.2 , 0.1 ], show_curve = False )
271
222
272
223
# 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()
277
226
```
278
227
279
- <!-- #region {"deletable": true, "editable": true} -->
280
228
#### Plot Only Hist and Curve
281
- <!-- #endregion -->
282
229
283
- ``` python deletable=true editable=true
284
- import plotly.plotly as py
230
+ ``` python
285
231
import plotly.figure_factory as ff
286
-
287
232
import numpy as np
288
233
289
234
x1 = np.random.randn(200 ) - 2
@@ -300,56 +245,28 @@ fig = ff.create_distplot(hist_data, group_labels, colors=colors,
300
245
bin_size = .2 , show_rug = False )
301
246
302
247
# 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()
307
250
```
308
251
309
- <!-- #region {"deletable": true, "editable": true} -->
310
252
#### Distplot with Pandas
311
- <!-- #endregion -->
312
253
313
- ``` python deletable=true editable=true
314
- import plotly.plotly as py
254
+ ``` python
315
255
import plotly.figure_factory as ff
316
-
317
256
import numpy as np
318
257
import pandas as pd
319
258
320
259
df = pd.DataFrame({' 2012' : np.random.randn(200 ),
321
260
' 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( )
324
263
```
325
264
326
- <!-- #region {"deletable": true, "editable": true} -->
327
265
#### Reference
328
- <!-- #endregion -->
329
266
330
- ``` python deletable=true editable=true
267
+ ``` python
331
268
help (ff.create_distplot)
332
269
```
270
+ ``` python
333
271
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' )
351
272
```
352
-
353
- ``` python deletable=true editable=true
354
-
355
- ```
0 commit comments