@@ -313,14 +313,14 @@ Stacked bar charts are a powerful way to present results summarizing categories
313313from plotly import graph_objects as go
314314import pandas as pd
315315
316- #get one year of gapminder data
316+ # Get one year of gapminder data
317317url = 'https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv'
318318df = pd.read_csv(url)
319319df = df[df['year']==2007]
320320df["gdp"]=df["pop"]*df['gdpPercap']
321321
322322
323- #build the summary of interest
323+ # Build the summary of interest
324324df_summarized = df.groupby("continent", observed=True).agg("sum").reset_index()
325325
326326df_summarized["percent of world population"]=100*df_summarized["pop"]/df_summarized["pop"].sum()
@@ -332,17 +332,17 @@ df = df_summarized[["continent",
332332"percent of world GDP",
333333]]
334334
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.
337337
338338fig=go.Figure()
339339for category in df_summarized["continent"].values:
340340 fig.add_trace(go.Bar(
341341 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
346346 y=list(df.loc[df["continent"]==category][list(df.columns[1:])].transpose().iloc[:,0]),
347347 name=str(category)
348348
0 commit comments