import pandas as pd
import matplotlib.pyplot as plt
# Sample data
data = {
'Segment': ['Consumer', 'Consumer', 'Consumer', 'Consumer',
'Corporate', 'Corporate', 'Corporate', 'Corporate',
'Home Office', 'Home Office', 'Home Office', 'Home Office'],
'Ship Mode': ['First Class', 'Same Day', 'Second Class', 'Standard Class',
'First Class', 'Same Day', 'Second Class', 'Standard Class',
'First Class', 'Same Day', 'Second Class', 'Standard Class'],
'Observed': [755, 312, 1003, 3031, 468, 114, 589, 1782, 278, 112, 310, 1046]
}
df = pd.DataFrame(data)
# Create a figure and axis with custom background color (outer background)
fig, ax = plt.subplots(figsize=(10, 6))
fig.patch.set_facecolor('#000000') # Set the outer background color to black
# Turn off axis lines
ax.axis('off')
# Create the table
table = ax.table(
cellText=df.values, # Data for the table
colLabels=df.columns, # Column headers
loc='center',
cellLoc='center', # Align text within cells
colWidths=[0.2, 0.3, 0.2], # Adjust column widths for better readability
bbox=[0.1, 0.2, 0.8, 0.7] # Position the table
)
# Set uniform grey color for all data rows
row_color = '#51414F' # Light grey background for data rows
# Apply background colors to the table cells
for (i, j), cell in table.get_celld().items():
if i == 0: # Header row
cell.set_facecolor('#581845') # Black background for header
cell.set_text_props(fontweight='bold', color='white') # Bold white text for header
else: # Data rows
cell.set_facecolor(row_color) # Light grey background for data rows
cell.set_text_props(color='white') # White text for data cells
# Add a title to the table
#ax.set_title("Observed Frequencies by Customer Segment and Ship Mode", fontsize=16, fontweight='bold', color='white')
# Set figure's background color to black
ax.set_facecolor('#000000') # Set inner plot background color to black to match outer
# Ensure proper layout
plt.tight_layout()
plt.show()