@@ -313,14 +313,14 @@ Stacked bar charts are a powerful way to present results summarizing categories
313
313
from plotly import graph_objects as go
314
314
import pandas as pd
315
315
316
- #get one year of gapminder data
316
+ # Get one year of gapminder data
317
317
url = 'https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv'
318
318
df = pd.read_csv(url)
319
319
df = df[df['year']==2007]
320
320
df["gdp"]=df["pop"]*df['gdpPercap']
321
321
322
322
323
- #build the summary of interest
323
+ # Build the summary of interest
324
324
df_summarized = df.groupby("continent", observed=True).agg("sum").reset_index()
325
325
326
326
df_summarized["percent of world population"]=100*df_summarized["pop"]/df_summarized["pop"].sum()
@@ -332,17 +332,17 @@ df = df_summarized[["continent",
332
332
"percent of world GDP",
333
333
]]
334
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.
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
337
338
338
fig=go.Figure()
339
339
for category in df_summarized["continent"].values:
340
340
fig.add_trace(go.Bar(
341
341
x=df.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
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 transposing and using iloc to convert to a series
345
+ # Here, we assume that the bar element category variable is in column 0
346
346
y=list(df.loc[df["continent"]==category][list(df.columns[1:])].transpose().iloc[:,0]),
347
347
name=str(category)
348
348
0 commit comments