@@ -11,6 +11,16 @@ jupyter:
11
11
display_name : Python 3
12
12
language : python
13
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
14
24
plotly :
15
25
description : How to make Histograms in Python with Plotly.
16
26
display_as : statistical
@@ -25,6 +35,7 @@ jupyter:
25
35
redirect_from : /python/histogram-tutorial/
26
36
thumbnail : thumbnail/histogram.jpg
27
37
title : Python Histograms | plotly
38
+ v4upgrade : true
28
39
---
29
40
30
41
## Histogram with plotly express
@@ -134,9 +145,8 @@ import plotly.graph_objs as go
134
145
import numpy as np
135
146
136
147
x = np.random.randn(500 )
137
- data = [go.Histogram(x = x, histnorm = ' probability' )]
148
+ fig = go.Figure( data = [go.Histogram(x = x, histnorm = ' probability' )])
138
149
139
- fig = go.Figure(data = data)
140
150
fig.show()
141
151
```
142
152
@@ -149,9 +159,8 @@ import numpy as np
149
159
150
160
y = np.random.randn(500 )
151
161
# Use `y` argument instead of `x` for horizontal histogram
152
- data = [go.Histogram(y = y)]
153
162
154
- fig = go.Figure(data = data )
163
+ fig = go.Figure(data = [go.Histogram( y = y)] )
155
164
fig.show()
156
165
```
157
166
@@ -166,10 +175,10 @@ x0 = np.random.randn(500)
166
175
# Add 1 to shift the mean of the Gaussian distribution
167
176
x1 = np.random.randn(500 ) + 1
168
177
169
- trace0 = go.Histogram(x = x0)
170
- trace1 = go.Histogram(x = x1)
178
+ fig = go.Figure()
179
+ fig.add_trace(go.Histogram(x = x0))
180
+ fig.add_trace(go.Histogram(x = x1))
171
181
172
- fig = go.Figure(data = [trace0, trace1])
173
182
# Overlay both histograms
174
183
fig.update(layout_barmode = ' overlay' )
175
184
# Reduce opacity to see both histograms
@@ -186,10 +195,10 @@ import numpy as np
186
195
x0 = np.random.randn(2000 )
187
196
x1 = np.random.randn(2000 ) + 1
188
197
189
- trace0 = go.Histogram(x = x0)
190
- trace1 = go.Histogram(x = x1)
198
+ fig = go.Figure()
199
+ fig.add_trace(go.Histogram(x = x0))
200
+ fig.add_trace(go.Histogram(x = x1))
191
201
192
- fig = go.Figure(data = [trace0, trace1])
193
202
# The two histograms are drawn on top of another
194
203
fig.update(layout_barmode = ' stack' )
195
204
```
@@ -203,7 +212,8 @@ import numpy as np
203
212
x0 = np.random.randn(500 )
204
213
x1 = np.random.randn(500 ) + 1
205
214
206
- trace0 = go.Histogram(
215
+ fig = go.Figure()
216
+ fig.add_trace(go.Histogram(
207
217
x = x0,
208
218
histnorm = ' percent' ,
209
219
name = ' control' , # name used in legend and hover labels
@@ -214,8 +224,8 @@ trace0 = go.Histogram(
214
224
),
215
225
marker_color = ' #EB89B5' ,
216
226
opacity = 0.75
217
- )
218
- trace1 = go.Histogram(
227
+ ))
228
+ fig.add_trace( go.Histogram(
219
229
x = x1,
220
230
histnorm = ' percent' ,
221
231
name = ' experimental' ,
@@ -226,16 +236,16 @@ trace1 = go.Histogram(
226
236
),
227
237
marker_color = ' #330C73' ,
228
238
opacity = 0.75
229
- )
239
+ ))
230
240
231
- layout = go.Layout(
241
+ fig.update( layout = go.Layout(
232
242
title = ' Sampled Results' , # title of plot
233
243
xaxis_title = ' Value' , # xaxis label
234
244
yaxis_title = ' Count' , # yaxis label
235
245
bargap = 0.2 , # gap between bars of adjacent location coordinates
236
246
bargroupgap = 0.1 # gap between bars of the same location coordinates
237
- )
238
- fig = go.Figure( data = [trace0, trace1], layout = layout)
247
+ ))
248
+
239
249
fig.show()
240
250
```
241
251
@@ -247,9 +257,8 @@ import plotly.graph_objs as go
247
257
import numpy as np
248
258
249
259
x = np.random.randn(500 )
250
- data = [go.Histogram(x = x, cumulative_enabled = True )]
260
+ fig = go.Figure( data = [go.Histogram(x = x, cumulative_enabled = True )])
251
261
252
- fig = go.Figure(data = data)
253
262
fig.show()
254
263
```
255
264
@@ -261,23 +270,21 @@ import plotly.graph_objs as go
261
270
x = [" Apples" ," Apples" ," Apples" ," Oranges" , " Bananas" ]
262
271
y = [" 5" ," 10" ," 3" ," 10" ," 5" ]
263
272
264
- data = [
265
- go.Histogram(
273
+ fig = go.Figure()
274
+ fig.add_trace( go.Histogram(
266
275
histfunc = " count" ,
267
276
y = y,
268
277
x = x,
269
278
name = " count"
270
- ),
271
- go.Histogram(
279
+ ))
280
+ fig.add_trace( go.Histogram(
272
281
histfunc = " sum" ,
273
282
y = y,
274
283
x = x,
275
284
name = " sum"
276
- )
277
- ]
285
+ ))
278
286
279
- fig = go.Figure(data = data)
280
- fig
287
+ fig.show()
281
288
```
282
289
283
290
### Custom Binning
@@ -290,6 +297,7 @@ from plotly.subplots import make_subplots
290
297
x = [' 1970-01-01' , ' 1970-01-01' , ' 1970-02-01' , ' 1970-04-01' , ' 1970-01-02' ,
291
298
' 1972-01-31' , ' 1970-02-13' , ' 1971-04-19' ]
292
299
300
+ fig = make_subplots(rows = 3 , cols = 2 )
293
301
294
302
trace0 = go.Histogram(x = x, nbinsx = 4 )
295
303
trace1 = go.Histogram(x = x, nbinsx = 8 )
@@ -316,7 +324,6 @@ trace5 = go.Histogram(x=x,
316
324
autobinx = False
317
325
)
318
326
319
- fig = make_subplots(rows = 3 , cols = 2 )
320
327
fig.append_trace(trace0, 1 , 1 )
321
328
fig.append_trace(trace1, 1 , 2 )
322
329
fig.append_trace(trace2, 2 , 1 )
0 commit comments