import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
# Step 1: Generate sample data for demonstration
# Assuming 'Working Capital' is a metric (e.g., Net Working Capital in $M) by Plant and Customer
# In a real scenario, load this from your database/CSV (e.g., df = pd.read_csv('your_data.csv'))
np.random.seed(42) # For reproducible results
plants = ['Plant A', 'Plant B', 'Plant C', 'Plant D']
customers = ['Customer X', 'Customer Y', 'Customer Z', 'Customer W', 'Customer V']
data = []
for plant in plants:
for customer in customers:
# Simulate working capital values (positive/negative for color variation)
wc = np.random.normal(50, 25, 1)[0] # Mean $50M, std $25M
data.append({'Plant': plant, 'Customer': customer, 'Working_Capital_M': round(wc, 2)})
df = pd.DataFrame(data)
print("Sample DataFrame:")
print(df.head(10))
print("\nFull DataFrame shape:", df.shape)
# Step 2: Pivot the data for heatmap (rows: Plants, columns: Customers, values: Working Capital)
pivot_df = df.pivot(index='Plant', columns='Customer', values='Working_Capital_M')
print("\nPivot Table:")
print(pivot_df)
# Step 3: Create the heatmap
plt.figure(figsize=(10, 6))
sns.heatmap(
pivot_df,
annot=True, # Show values in cells
cmap='RdYlGn', # Red-Yellow-Green colormap (red=low/bad, green=high/good)
center=50, # Center the color scale around 50 (adjust based on your data)
fmt='.1f', # Format numbers to 1 decimal
cbar_kws={'label': 'Working Capital ($M)'},
linewidths=0.5 # Add grid lines for clarity
)
plt.title('Working Capital Heatmap by Plant and Customer', fontsize=16, pad=20)
plt.xlabel('Customer', fontsize=12)
plt.ylabel('Plant', fontsize=12)
plt.tight_layout()
plt.show()
# Optional: Save the heatmap as an image
# plt.savefig('working_capital_heatmap.png', dpi=300, bbox_inches='tight')
How to Use This:
- Run in Jupyter Notebook, Google Colab, or any Python environment with
pandas, seaborn, matplotlib, and numpy installed (pip install pandas seaborn matplotlib numpy).
- Customize with Real Data:
- Key Features:
- Pivot: Aggregates to mean by default; use
pivot_table for avg/sum if duplicates: df.pivot_table(index='Plant', columns='Customer', values='Working_Capital_M', aggfunc='mean').
- Colors: Red for low/negative WC (poor), Green for high (healthy).
- Scalable: Handles missing data (shows NaN as empty/light cells).
This produces a fully working, interactive heatmap when executed. If you provide your actual data (CSV snippet or description), I can refine it! 🕵️♂️