Skip to content

Commit 862c310

Browse files
committed
graph_objects, update_layout
1 parent 4d285ae commit 862c310

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

notebooks/histograms.md

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jupyter:
4040

4141
## Histogram with plotly express
4242

43-
A [histogram](https://en.wikipedia.org/wiki/Histogram) is representation of the distribution of numerical data, where the data are binned and the count for each bin is represented.
43+
In statistics, a [histogram](https://en.wikipedia.org/wiki/Histogram) is representation of the distribution of numerical data, where the data are binned and the count for each bin is represented. More generally, in plotly a histogram is an aggregated bar chart, with several possible aggregation functions (e.g. sum, average, count...). Also, the data to be binned can be numerical data but also categorical or date data.
4444

4545
Plotly express functions take as argument a tidy [pandas DataFrame](https://pandas.pydata.org/pandas-docs/stable/getting_started/10min.html).
4646

@@ -51,6 +51,14 @@ fig = px.histogram(tips, x="total_bill")
5151
fig.show()
5252
```
5353

54+
```python
55+
import plotly.express as px
56+
tips = px.data.tips()
57+
# Here we use a column with categorical data
58+
fig = px.histogram(tips, x="day")
59+
fig.show()
60+
```
61+
5462
#### Choosing the number of bins
5563

5664
By default, the number of bins is chosen so that this number is comparable to the typical number of samples in a bin. This number can be customized, as well as the range of values.
@@ -81,9 +89,9 @@ tips = px.data.tips()
8189
fig = px.histogram(tips, x="total_bill",
8290
title='Histogram of bills',
8391
labels={'total_bill':'total bill'}, # can specify one label per df column
84-
opacity=0.6,
92+
opacity=0.8,
8593
log_y=True, # represent bars with log scale
86-
color_discrete_sequence=['red'] # color of histogram bars
94+
color_discrete_sequence=['indianred'] # color of histogram bars
8795
)
8896
fig.show()
8997
```
@@ -122,12 +130,12 @@ fig.show()
122130

123131
## Histograms with go.Histogram
124132

125-
When data are not available as tidy dataframes, it is also possible to use the more generic `go.Histogram` from `plotly.graph_objs`. All of the available histogram options are described in the histogram section of the reference page: https://plot.ly/python/reference#histogram.
133+
When data are not available as tidy dataframes, it is also possible to use the more generic `go.Histogram` from `plotly.graph_objects`. All of the available histogram options are described in the histogram section of the reference page: https://plot.ly/python/reference#histogram.
126134

127135
### Basic Histogram ###
128136

129137
```python
130-
import plotly.graph_objs as go
138+
import plotly.graph_objects as go
131139

132140
import numpy as np
133141

@@ -140,7 +148,7 @@ fig.show()
140148
### Normalized Histogram
141149

142150
```python
143-
import plotly.graph_objs as go
151+
import plotly.graph_objects as go
144152

145153
import numpy as np
146154

@@ -153,7 +161,7 @@ fig.show()
153161
### Horizontal Histogram ###
154162

155163
```python
156-
import plotly.graph_objs as go
164+
import plotly.graph_objects as go
157165

158166
import numpy as np
159167

@@ -167,7 +175,7 @@ fig.show()
167175
### Overlaid Histogram ###
168176

169177
```python
170-
import plotly.graph_objs as go
178+
import plotly.graph_objects as go
171179

172180
import numpy as np
173181

@@ -180,15 +188,16 @@ fig.add_trace(go.Histogram(x=x0))
180188
fig.add_trace(go.Histogram(x=x1))
181189

182190
# Overlay both histograms
183-
fig.update(layout_barmode='overlay')
191+
fig.update_layout(barmode='overlay')
184192
# Reduce opacity to see both histograms
185193
fig.update_traces(opacity=0.75)
194+
fig.show()
186195
```
187196

188197
### Stacked Histograms
189198

190199
```python
191-
import plotly.graph_objs as go
200+
import plotly.graph_objects as go
192201

193202
import numpy as np
194203

@@ -200,13 +209,14 @@ fig.add_trace(go.Histogram(x=x0))
200209
fig.add_trace(go.Histogram(x=x1))
201210

202211
# The two histograms are drawn on top of another
203-
fig.update(layout_barmode='stack')
212+
fig.update_layout(barmode='stack')
213+
fig.show()
204214
```
205215

206216
### Styled Histogram
207217

208218
```python
209-
import plotly.graph_objs as go
219+
import plotly.graph_objects as go
210220

211221
import numpy as np
212222
x0 = np.random.randn(500)
@@ -238,21 +248,21 @@ fig.add_trace(go.Histogram(
238248
opacity=0.75
239249
))
240250

241-
fig.update(layout=go.Layout(
242-
title='Sampled Results', # title of plot
243-
xaxis_title='Value', # xaxis label
244-
yaxis_title='Count', # yaxis label
251+
fig.update_layout(
252+
title_text='Sampled Results', # title of plot
253+
xaxis_title_text='Value', # xaxis label
254+
yaxis_title_text='Count', # yaxis label
245255
bargap=0.2, # gap between bars of adjacent location coordinates
246256
bargroupgap=0.1 # gap between bars of the same location coordinates
247-
))
257+
)
248258

249259
fig.show()
250260
```
251261

252262
### Cumulative Histogram
253263

254264
```python
255-
import plotly.graph_objs as go
265+
import plotly.graph_objects as go
256266

257267
import numpy as np
258268

@@ -262,27 +272,17 @@ fig = go.Figure(data=[go.Histogram(x=x, cumulative_enabled=True)])
262272
fig.show()
263273
```
264274

265-
### Specify Binning Function
275+
### Specify Aggregation Function
266276

267277
```python
268-
import plotly.graph_objs as go
278+
import plotly.graph_objects as go
269279

270280
x = ["Apples","Apples","Apples","Oranges", "Bananas"]
271281
y = ["5","10","3","10","5"]
272282

273283
fig = go.Figure()
274-
fig.add_trace(go.Histogram(
275-
histfunc = "count",
276-
y=y,
277-
x=x,
278-
name="count"
279-
))
280-
fig.add_trace(go.Histogram(
281-
histfunc="sum",
282-
y=y,
283-
x=x,
284-
name="sum"
285-
))
284+
fig.add_trace(go.Histogram(histfunc="count", y=y, x=x, name="count"))
285+
fig.add_trace(go.Histogram(histfunc="sum", y=y, x=x, name="sum"))
286286

287287
fig.show()
288288
```
@@ -291,7 +291,7 @@ fig.show()
291291
For custom binning along x-axis, use the attribute [`nbinsx`](https://plot.ly/python/reference/#histogram-nbinsx). Please note that the autobin algorithm will choose a 'nice' round bin size that may result in somewhat fewer than `nbinsx` total bins. Alternatively, you can set the exact values for [`xbins`](https://plot.ly/python/reference/#histogram-xbins) along with `autobinx = False`.
292292

293293
```python
294-
import plotly.graph_objs as go
294+
import plotly.graph_objects as go
295295
from plotly.subplots import make_subplots
296296

297297
x = ['1970-01-01', '1970-01-01', '1970-02-01', '1970-04-01', '1970-01-02',

0 commit comments

Comments
 (0)