liveBook · Manning

Stacked bar chart

As explained in Starting Data Analytics with Generative AI and Python

A stacked bar plot displays multiple component values as segments accumulated within each bar.

A stacked bar chart accumulates several component values as colored segments inside each bar. It answers questions such as how order statuses contribute to totals across product categories. section 3.3.4

The practical workflow is to reshape grouped data into a pivot table, use categories as the index, use components as columns, and plot with pandas using kind='bar' and stacked=True. section 3.3.4

Use the chart carefully:

  • Compare total bar heights to see overall volume, then inspect segment sizes to study composition. section 3.3.4
  • Reshape the grouped DataFrame before plotting; the example pivots product categories against order statuses and sums counts. section 3.3.4
  • Improve readability with a larger figure, rotated category labels, adjusted font sizes, axis labels, a title, and a legend. section 3.3.4
  • Treat a crowded or highly imbalanced chart skeptically: many categories are difficult to read, and dominant segments can hide smaller statuses. section 3.3.4 section 3.3.3

Order status by product category

After grouping merged order data by order status, English product category, and seller, pivot the counts so each category becomes a bar and each status becomes a stacked segment. The resulting visualization compares category totals while showing that delivered orders dominate across categories; smaller statuses may require further investigation. section 3.3.4

import pandas as pd
import matplotlib.pyplot as plt

# Pivot the DataFrame for the stacked bar chart
status_pivot = 
status_distribution_sorted.pivot_table(
index='product_category_name_english',
    columns='order_status',
    values='count',
    aggfunc='sum',
    fill_value=0)

# Plot the stacked bar chart
plt.figure(figsize=(20, 10))
status_pivot.plot(kind='bar', stacked=True, figsize=(20, 10))

plt.xticks(rotation=90, fontsize=10)
plt.yticks(fontsize=12)
plt.xlabel('Product Category', fontsize=12)
plt.ylabel('Count of Orders', fontsize=12)
plt.title('Order Status Distribution by Product Category', fontsize=14)
plt.legend(title='Order Status', title_fontsize=12, fontsize=10, 
loc='upper right')

plt.tight_layout()
plt.show()

Inspect the complete pandas and Matplotlib workflow for pivoting order-status counts and producing the stacked chart. section 3.3.4

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Explains pivoting, plotting, formatting, interpretation, and readability limits, but does not develop statistical theory.
Examples
Demonstrates order statuses by product category and discusses stacked alternatives for product popularity and review-score distributions.
What it assumes
Assumes grouped DataFrames and merged datasets, while demonstrating the reshape-and-plot workflow explicitly.
What you can do after
Apply it via pandas and Matplotlib to compare totals and component distributions, then inspect crowded or misleading outputs.
How it connects
Connects stacked bar charts with pandas, Matplotlib, DataFrames, pivot tables, grouping, and order-status analysis.

Also explained in

Compare how 5 books explain stacked bar chart →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.