Skip to content

Commit a15e562

Browse files
committed
some inlining
1 parent f9ee8b1 commit a15e562

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

notebooks/histograms.md

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ jupyter:
1111
display_name: Python 3
1212
language: python
1313
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
1424
plotly:
1525
description: How to make Histograms in Python with Plotly.
1626
display_as: statistical
@@ -25,6 +35,7 @@ jupyter:
2535
redirect_from: /python/histogram-tutorial/
2636
thumbnail: thumbnail/histogram.jpg
2737
title: Python Histograms | plotly
38+
v4upgrade: true
2839
---
2940

3041
## Histogram with plotly express
@@ -134,9 +145,8 @@ import plotly.graph_objs as go
134145
import numpy as np
135146

136147
x = np.random.randn(500)
137-
data = [go.Histogram(x=x, histnorm='probability')]
148+
fig = go.Figure(data=[go.Histogram(x=x, histnorm='probability')])
138149

139-
fig = go.Figure(data=data)
140150
fig.show()
141151
```
142152

@@ -149,9 +159,8 @@ import numpy as np
149159

150160
y = np.random.randn(500)
151161
# Use `y` argument instead of `x` for horizontal histogram
152-
data = [go.Histogram(y=y)]
153162

154-
fig = go.Figure(data=data)
163+
fig = go.Figure(data=[go.Histogram(y=y)])
155164
fig.show()
156165
```
157166

@@ -166,10 +175,10 @@ x0 = np.random.randn(500)
166175
# Add 1 to shift the mean of the Gaussian distribution
167176
x1 = np.random.randn(500) + 1
168177

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))
171181

172-
fig = go.Figure(data=[trace0, trace1])
173182
# Overlay both histograms
174183
fig.update(layout_barmode='overlay')
175184
# Reduce opacity to see both histograms
@@ -186,10 +195,10 @@ import numpy as np
186195
x0 = np.random.randn(2000)
187196
x1 = np.random.randn(2000) + 1
188197

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))
191201

192-
fig = go.Figure(data=[trace0, trace1])
193202
# The two histograms are drawn on top of another
194203
fig.update(layout_barmode='stack')
195204
```
@@ -203,7 +212,8 @@ import numpy as np
203212
x0 = np.random.randn(500)
204213
x1 = np.random.randn(500) + 1
205214

206-
trace0 = go.Histogram(
215+
fig = go.Figure()
216+
fig.add_trace(go.Histogram(
207217
x=x0,
208218
histnorm='percent',
209219
name='control', # name used in legend and hover labels
@@ -214,8 +224,8 @@ trace0 = go.Histogram(
214224
),
215225
marker_color='#EB89B5',
216226
opacity=0.75
217-
)
218-
trace1 = go.Histogram(
227+
))
228+
fig.add_trace(go.Histogram(
219229
x=x1,
220230
histnorm='percent',
221231
name='experimental',
@@ -226,16 +236,16 @@ trace1 = go.Histogram(
226236
),
227237
marker_color='#330C73',
228238
opacity=0.75
229-
)
239+
))
230240

231-
layout = go.Layout(
241+
fig.update(layout=go.Layout(
232242
title='Sampled Results', # title of plot
233243
xaxis_title='Value', # xaxis label
234244
yaxis_title='Count', # yaxis label
235245
bargap=0.2, # gap between bars of adjacent location coordinates
236246
bargroupgap=0.1 # gap between bars of the same location coordinates
237-
)
238-
fig = go.Figure(data=[trace0, trace1], layout=layout)
247+
))
248+
239249
fig.show()
240250
```
241251

@@ -247,9 +257,8 @@ import plotly.graph_objs as go
247257
import numpy as np
248258

249259
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)])
251261

252-
fig = go.Figure(data=data)
253262
fig.show()
254263
```
255264

@@ -261,23 +270,21 @@ import plotly.graph_objs as go
261270
x = ["Apples","Apples","Apples","Oranges", "Bananas"]
262271
y = ["5","10","3","10","5"]
263272

264-
data = [
265-
go.Histogram(
273+
fig = go.Figure()
274+
fig.add_trace(go.Histogram(
266275
histfunc = "count",
267276
y=y,
268277
x=x,
269278
name="count"
270-
),
271-
go.Histogram(
279+
))
280+
fig.add_trace(go.Histogram(
272281
histfunc="sum",
273282
y=y,
274283
x=x,
275284
name="sum"
276-
)
277-
]
285+
))
278286

279-
fig = go.Figure(data=data)
280-
fig
287+
fig.show()
281288
```
282289

283290
### Custom Binning
@@ -290,6 +297,7 @@ from plotly.subplots import make_subplots
290297
x = ['1970-01-01', '1970-01-01', '1970-02-01', '1970-04-01', '1970-01-02',
291298
'1972-01-31', '1970-02-13', '1971-04-19']
292299

300+
fig = make_subplots(rows=3, cols=2)
293301

294302
trace0 = go.Histogram(x=x, nbinsx=4)
295303
trace1 = go.Histogram(x=x, nbinsx = 8)
@@ -316,7 +324,6 @@ trace5 = go.Histogram(x=x,
316324
autobinx = False
317325
)
318326

319-
fig = make_subplots(rows=3, cols=2)
320327
fig.append_trace(trace0, 1, 1)
321328
fig.append_trace(trace1, 1, 2)
322329
fig.append_trace(trace2, 2, 1)

0 commit comments

Comments
 (0)