Skip to content

Commit dccbdf8

Browse files
proposed addition of a df.agg stacked go.bar example
1 parent 39b8dac commit dccbdf8

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

doc/python/bar-charts.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,57 @@ fig.update_layout(barmode='stack')
304304
fig.show()
305305
```
306306

307+
### Stacked Bar Chart from aggregating a data frame
308+
309+
Stacked bar charts are a powerful way to present results summarizing categories generated using the Pandas aggregate commands, which produce a wide format data set with one row for each bar component and a column for each bar, which is the transpose of the orientation of the px.bar wide data frame. Tranposing and updating the indexes is a somewhat involved option. Here is one straightforward way to aggregate a data set into a summarized form and present the results as a stacked bar.
310+
311+
```
312+
313+
from plotly import graph_objects as go
314+
import pandas as pd
315+
316+
#get one year of gapminder data
317+
url = 'https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv'
318+
df = pd.read_csv(url)
319+
df = df[df['year']==2007]
320+
df["gdp"]=df["pop"]*df['gdpPercap']
321+
322+
323+
#build the summary of interest
324+
df_summarized = df.groupby("continent", observed=True).agg("sum").reset_index()
325+
326+
df_summarized["percent of world population"]=100*df_summarized["pop"]/df_summarized["pop"].sum()
327+
df_summarized["percent of world GDP"]=100*df_summarized["gdp"]/df_summarized["gdp"].sum()
328+
329+
330+
df2 = df_summarized[["continent",
331+
"percent of world population",
332+
"percent of world GDP",
333+
]]
334+
335+
#we now have a wide data frame, but it's in the opposite orientation from the one that px is designed to deal with.
336+
#transposing it and rebuilding the indexes is an option, but iterating through the DF using graph objects is more succinct.
337+
338+
fig=go.Figure()
339+
for category in df_summarized["continent"].values:
340+
fig.add_trace(go.Bar(
341+
x=df2.columns[1:],
342+
#we need to get a pandas series that contains just the values to graph;
343+
#we do so by selecting the right row, selecting the right columns
344+
#and then tranposing and using iloc to convert to a series
345+
#here, I assume that the bar element category variable is in column 0
346+
y=list(df2.loc[df2["continent"]==category][list(df2.columns[1:])].transpose().iloc[:,0]),
347+
name=str(category)
348+
349+
350+
)
351+
)
352+
fig.update_layout(barmode="stack")
353+
354+
fig.show()
355+
```
356+
357+
307358
### Bar Chart with Hover Text
308359

309360
```python

0 commit comments

Comments
 (0)