Bar Plot Gallery¶
The bar plot is used for visualizing categorical data with rectangular bars. It's ideal for comparing values across different categories, showing distributions, and displaying grouped comparisons.
Bar plots excel at:
- Category comparison: Compare values across different categories (e.g., sales by product)
- Grouped comparisons: Show multiple metrics side by side for easy comparison
- Data distribution: Visualize the distribution of categorical data
- Ranking visualization: Display sorted data to show top/bottom performers
import matplotlib.pyplot as plt
import pandas as pd
from pyretailscience.plots import bar
# Create sample product sales data
# fmt: off
product_sales = pd.DataFrame(
{
"sales": [125000, 98000, 156000, 87000, 134000, 112000],
},
index=["Electronics", "Clothing", "Home & Garden", "Sports", "Books", "Toys"],
)
# fmt: on
ax = bar.plot(
product_sales,
value_col="sales",
title="Product Category Sales",
x_label="Product Category",
y_label="Sales ($)",
rot=0,
)
plt.show()
Using x_col Parameter¶
Specify a column to use as the x-axis categories instead of the DataFrame index.
# Create sample store performance data
# fmt: off
store_performance = pd.DataFrame(
{
"store_id": ["ST001", "ST002", "ST003", "ST004", "ST005"],
"revenue": [245000, 189000, 267000, 198000, 221000],
},
)
# fmt: on
ax = bar.plot(
store_performance,
x_col="store_id",
value_col="revenue",
title="Store Revenue Performance",
x_label="Store ID",
y_label="Revenue ($)",
rot=0,
)
plt.show()
Multiple Value Columns (Grouped Bars)¶
Create grouped bar plots by passing a list to value_col. This shows multiple metrics side by side for easy comparison.
# Create sample quarterly performance data
# fmt: off
quarterly_data = pd.DataFrame(
{
"quarter": ["Q1", "Q2", "Q3", "Q4"],
"Online Sales": [180000, 195000, 210000, 225000],
"Store Sales": [320000, 315000, 285000, 340000],
"Wholesale": [95000, 105000, 115000, 108000],
},
)
# fmt: on
ax = bar.plot(
quarterly_data,
x_col="quarter",
value_col=["Online Sales", "Store Sales", "Wholesale"],
title="Quarterly Sales by Channel",
x_label="Quarter",
y_label="Sales ($)",
legend_title="Sales Channel",
move_legend_outside=True,
rot=0,
)
plt.show()
Horizontal Bar Plot¶
Create horizontal bars using the orientation parameter. Useful when category names are long.
# Create sample product category data with long names
# fmt: off
category_revenue = pd.DataFrame(
{
"category": [
"Consumer Electronics", "Home & Kitchen Appliances", "Sporting Goods & Outdoors",
"Health & Personal Care", "Books & Media",
],
"revenue": [425000, 312000, 198000, 267000, 145000],
},
)
# fmt: on
ax = bar.plot(
category_revenue,
x_col="category",
value_col="revenue",
title="Revenue by Product Category",
x_label="Revenue ($)",
y_label="Product Category",
orientation="horizontal",
)
plt.show()
Sorted Bar Plot¶
Sort bars in ascending or descending order using the sort_order parameter to show rankings.
# Create sample sales representative performance data
# fmt: off
sales_rep_data = pd.DataFrame(
{
"sales_rep": ["Alice", "Bob", "Charlie", "Diana", "Eve", "Frank"],
"sales_amount": [89000, 134000, 67000, 156000, 98000, 112000],
},
)
# fmt: on
ax = bar.plot(
sales_rep_data,
x_col="sales_rep",
value_col="sales_amount",
title="Top Sales Representatives (Descending)",
x_label="Sales Representative",
y_label="Sales Amount ($)",
sort_order="descending",
rot=0,
)
plt.show()
Bar Plot with Data Labels¶
Add data labels to bars showing absolute values or percentages using the data_label_format parameter.
# Create sample regional sales data
# fmt: off
regional_sales = pd.DataFrame(
{
"region": ["North", "South", "East", "West"],
"sales": [285000, 231000, 198000, 267000],
},
)
# fmt: on
ax = bar.plot(
regional_sales,
x_col="region",
value_col="sales",
title="Regional Sales with Data Labels",
x_label="Region",
y_label="Sales ($)",
data_label_format="absolute",
rot=0,
)
plt.show()
Percentage Data Labels by Series¶
Show percentage contribution of each bar to the total using data_label_format="percentage_by_series".
# Create sample market share data
# fmt: off
market_share_data = pd.DataFrame(
{
"brand": ["Brand A", "Brand B", "Brand C", "Brand D", "Brand E"],
"market_share": [35, 28, 18, 12, 7],
},
)
# fmt: on
ax = bar.plot(
market_share_data,
x_col="brand",
value_col="market_share",
title="Market Share Distribution",
x_label="Brand",
y_label="Market Share (%)",
data_label_format="percentage_by_series",
rot=0,
)
plt.show()
Bar Plot with Hatch Patterns¶
Apply hatch patterns to bars for enhanced visual differentiation using the use_hatch parameter.
# Create sample monthly performance data
# fmt: off
monthly_performance = pd.DataFrame(
{
"month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
"Target Sales": [150000, 145000, 160000, 155000, 165000, 170000],
"Actual Sales": [142000, 138000, 168000, 149000, 172000, 163000],
},
)
# fmt: on
ax = bar.plot(
monthly_performance,
x_col="month",
value_col=["Target Sales", "Actual Sales"],
title="Monthly Sales: Target vs Actual",
x_label="Month",
y_label="Sales ($)",
legend_title="Sales Type",
use_hatch=True,
move_legend_outside=True,
rot=0,
)
plt.show()
Plotting a Pandas Series¶
You can also plot a pandas Series directly, which automatically uses the Series name and index.
# Create a pandas Series
# fmt: off
customer_satisfaction = pd.Series(
[4.2, 4.5, 3.8, 4.1, 4.7, 3.9, 4.3],
index=["Service", "Quality", "Price", "Delivery", "Support", "Returns", "Overall"],
name="Satisfaction Score",
)
# fmt: on
ax = bar.plot(
customer_satisfaction,
value_col="Satisfaction Score",
title="Customer Satisfaction Ratings",
x_label="Category",
y_label="Rating (1-5)",
rot=0,
)
plt.show()
Advanced Styling Options¶
Customize the appearance with source attribution, legend positioning, and custom styling.
# Create sample department performance data
# fmt: off
department_data = pd.DataFrame(
{
"department": ["Sales", "Marketing", "Operations", "Customer Service"],
"Q1 Performance": [92, 88, 95, 87],
"Q2 Performance": [89, 91, 93, 90],
"Q3 Performance": [94, 85, 91, 92],
},
)
# fmt: on
ax = bar.plot(
department_data,
x_col="department",
value_col=["Q1 Performance", "Q2 Performance", "Q3 Performance"],
title="Department Performance by Quarter",
x_label="Department",
y_label="Performance Score",
legend_title="Quarter",
move_legend_outside=True,
source_text="Source: Internal Performance Dashboard",
rot=0,
)
plt.show()