Skip to content

Commit c88e191

Browse files
committed
addressed Nicolas' comments
1 parent 4008ad0 commit c88e191

File tree

1 file changed

+53
-37
lines changed

1 file changed

+53
-37
lines changed

notebooks/bar-charts.md

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ In a bar plot, each row of the DataFrame is represented as a rectangular mark.
2424

2525
```python
2626
import plotly_express as px
27-
data = px.data.gapminder()
28-
data_canada = data[data.country == 'Canada']
29-
px.bar(data_canada, x='year', y='pop')
27+
data_canada = px.data.gapminder().query("country == 'Canada'")
28+
fig = px.bar(data_canada, x='year', y='pop')
29+
fig.show()
3030
```
3131

3232
```python
@@ -35,46 +35,60 @@ data_canada
3535

3636
### Customize bar chart with plotly express
3737

38+
The bar plot can be customized using keyword arguments *TODO here link to meta doc page on customizing plotly plots?*.
39+
3840
```python
3941
import plotly_express as px
4042
data = px.data.gapminder()
4143

4244
data_canada = data[data.country == 'Canada']
43-
px.bar(data_canada, x='year', y='pop',
44-
hover_data=['lifeExp', 'gdpPercap'], color='lifeExp',
45-
labels={'pop':'population of Canada'}, height=400)
45+
fig = px.bar(data_canada, x='year', y='pop',
46+
hover_data=['lifeExp', 'gdpPercap'], color='lifeExp',
47+
labels={'pop':'population of Canada'}, height=400)
48+
fig.show()
4649
```
4750

51+
When several rows share the same value of `x` (here Female or Male), the rectangles are stacked on top of one another by default.
52+
4853
```python
4954
import plotly_express as px
5055
tips = px.data.tips()
51-
px.bar(tips, x="sex", y="total_bill", color='time')
56+
fig = px.bar(tips, x="sex", y="total_bill", color='time')
57+
fig.show()
5258
```
5359

5460
```python
55-
px.bar(tips, x="sex", y="total_bill", color='smoker', barmode='group',
61+
# Change the default stacking
62+
fig = px.bar(tips, x="sex", y="total_bill", color='smoker', barmode='group',
5663
height=400)
64+
fig.show()
5765
```
5866

67+
#### Facetted subplots
68+
69+
Use the keyword arguments `facet_row` (resp. `facet_col`) to create facetted subplots, where different rows (resp. columns) correspond to different values of the dataframe column specified in `facet_row`.
70+
5971
```python
60-
px.bar(tips, x="sex", y="total_bill", color="smoker", barmode="group",
61-
facet_row="time", facet_col="day",
62-
category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]})
72+
fig = px.bar(tips, x="sex", y="total_bill", color="smoker", barmode="group",
73+
facet_row="time", facet_col="day",
74+
category_orders={"day": ["Thur", "Fri", "Sat", "Sun"],
75+
"time": ["Lunch", "Dinner"]})
76+
fig.show()
6377
```
6478

6579
To learn more, see the *link to px.bar reference page*.
6680

6781

6882
#### Basic Bar Chart with plotly.graph_objs
6983

70-
When data are not available as pandas DataFrame, it is also possible to use the more generic `go.Bar` function from `plotly.graph_objs`.
84+
When data are not available as tidy dataframes, it is also possible to use the more generic `go.Bar` function from `plotly.graph_objs`.
7185

7286
```python
7387
import plotly.graph_objs as go
7488
animals=['giraffes', 'orangutans', 'monkeys']
7589

7690
fig = go.Figure([go.Bar(x=animals, y=[20, 14, 23])])
77-
fig
91+
fig.show()
7892
```
7993

8094
#### Grouped Bar Chart
@@ -89,6 +103,7 @@ fig = go.Figure(data=[
89103
go.Bar(name='SF Zoo', x=animals, y=[20, 14, 23]),
90104
go.Bar(name='LA Zoo', x=animals, y=[12, 18, 29])
91105
])
106+
# Change the bar mode
92107
fig.update(layout_barmode='group')
93108
```
94109

@@ -102,6 +117,7 @@ fig = go.Figure(data=[
102117
go.Bar(name='SF Zoo', x=animals, y=[20, 14, 23]),
103118
go.Bar(name='LA Zoo', x=animals, y=[12, 18, 29])
104119
])
120+
# Change the bar mode
105121
fig.update(layout_barmode='stack')
106122
```
107123

@@ -113,15 +129,14 @@ import plotly.graph_objs as go
113129
x = ['Product A', 'Product B', 'Product C']
114130
y = [20, 14, 23]
115131

116-
data = [go.Bar(x=x, y=y,
117-
text=['27% market share', '24% market share', '19% market share'])]
118-
119-
fig = go.Figure(data=data)
132+
# Use the text kw argument for hover text
133+
fig = go.Figure(data=[go.Bar(x=x, y=y,
134+
text=['27% market share', '24% market share', '19% market share'])])
120135
# Customize aspect
121136
fig.update_traces(marker_color='rgb(158,202,225)', marker_line_color='rgb(8,48,107)',
122137
marker_line_width=1.5, opacity=0.6)
123138
fig.update(layout_title_text='January 2013 Sales Report')
124-
fig
139+
fig.show()
125140
```
126141

127142
### Bar Chart with Direct Labels
@@ -132,6 +147,7 @@ import plotly.graph_objs as go
132147
x = ['Product A', 'Product B', 'Product C']
133148
y = [20, 14, 23]
134149

150+
# Use textposition='auto' for direct text
135151
data = [go.Bar(
136152
x=x,
137153
y=y,
@@ -143,11 +159,11 @@ data = [go.Bar(
143159
color='rgb(8,48,107)',
144160
width=1.5),
145161
),
146-
opacity=0.6
162+
opacity=0.5
147163
)]
148164

149165
fig = go.Figure(data=data)
150-
fig
166+
fig.show()
151167
```
152168

153169
### Rotated Bar Chart Labels
@@ -171,11 +187,9 @@ trace1 = go.Bar(
171187
marker_color='rgb(204,204,204)'
172188
)
173189

174-
data = [trace0, trace1]
175-
176-
fig = go.Figure(data=data)
190+
fig = go.Figure(data=[trace0, trace1])
191+
# Here we modify the tickangle of the xaxis, resulting in rotated labels.
177192
fig.update(layout_barmode='group', layout_xaxis_tickangle=-45)
178-
fig
179193
```
180194

181195
### Customizing Individual Bar Colors
@@ -192,12 +206,11 @@ trace0 = go.Bar(
192206
x=['Feature A', 'Feature B', 'Feature C',
193207
'Feature D', 'Feature E'],
194208
y=[20, 14, 23, 25, 22],
195-
marker_color=colors
209+
marker_color=colors # marker color can be a single color value or an iterable
196210
)
197211

198212
fig = go.Figure(data=[trace0])
199213
fig.update(layout_title_text='Least Used Feature')
200-
fig
201214
```
202215

203216
### Customizing Individual Bar Widths
@@ -208,38 +221,39 @@ import plotly.graph_objs as go
208221
trace0 = go.Bar(
209222
x=[1, 2, 3, 5.5, 10],
210223
y=[10, 8, 6, 4, 2],
211-
width = [0.8, 0.8, 0.8, 3.5, 4]
224+
width=[0.8, 0.8, 0.8, 3.5, 4] # customize width here
212225
)
213226

214227
fig = go.Figure(data=[trace0])
215-
fig
228+
fig.show()
216229
```
217230

218231
### Customizing Individual Bar Base
219232

220-
*I find this example not very good, because I think I would instead plot negative values for the expenses instead of changing the base. Can we come up with a better idea?*
221233

222234
```python
223235
import plotly.graph_objs as go
224236

225237
years = ['2016','2017','2018']
226238

227239
trace0 = go.Bar(x=years, y=[500, 600, 700],
228-
base = [-500,-600,-700],
240+
base=[-500,-600,-700],
229241
marker_color='red',
230-
name = 'expenses')
242+
name='expenses')
231243
trace1 = go.Bar(x=years, y=[300, 400, 700],
232244
base=0,
233245
marker_color='blue',
234246
name='revenue'
235247
)
236248

237249
fig = go.Figure(data=[trace0, trace1])
238-
fig
250+
fig.show()
239251
```
240252

241253
### Colored and Styled Bar Chart
242254

255+
In this example several parameters of the layout as customized, hence it is convenient to use directly the `go.Layout(...)` constructor instead of calling `fig.update`.
256+
243257
```python
244258
import plotly.graph_objs as go
245259

@@ -285,12 +299,12 @@ layout = go.Layout(
285299
bordercolor='rgba(255, 255, 255, 0)'
286300
),
287301
barmode='group',
288-
bargap=0.15,
289-
bargroupgap=0.1
302+
bargap=0.15, # gap between bars of adjacent location coordinates.
303+
bargroupgap=0.1 # gap between bars of the same location coordinate.
290304
)
291305

292306
fig = go.Figure(data=data, layout=layout)
293-
fig
307+
fig.show()
294308
```
295309

296310
### Waterfall Bar Chart
@@ -346,11 +360,14 @@ for i in range(0, 7):
346360

347361
fig = go.Figure(data=data, layout=layout)
348362
fig.update_traces(marker_line_width=2)
349-
fig
363+
fig.show()
350364
```
351365

352366
### Bar Chart with Relative Barmode
353367

368+
With "relative" barmode, the bars are stacked on top of one another, with negative values
369+
below the axis, positive values above.
370+
354371
```python
355372
x = [1, 2, 3, 4]
356373

@@ -363,7 +380,6 @@ data = [trace0, trace1, trace2, trace3];
363380

364381
fig = go.Figure(data=data)
365382
fig.update(layout_barmode='relative', layout_title_text='Relative Barmode')
366-
fig
367383
```
368384

369385
### Horizontal Bar Charts

0 commit comments

Comments
 (0)