Python Analysis Code
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Load the tips dataset
tips = sns.load_dataset('tips')
# 1. Average Bill by Day
avg_bill = tips.groupby('day')['total_bill'].mean()
plt.figure(figsize=(8, 5))
sns.barplot(data=tips, x='day', y='total_bill',
order=['Thur', 'Fri', 'Sat', 'Sun'],
ci=None, palette='Blues_d')
plt.xlabel('Day of Week')
plt.ylabel('Average Bill ($)')
plt.title('Average Total Bill by Day')
plt.show()
# 2. Tip % by Party Size
tips['tip_pct'] = (tips['tip'] / tips['total_bill']) * 100
tip_by_size = tips.groupby('size')['tip_pct'].mean()
plt.figure(figsize=(8, 5))
plt.plot(tip_by_size.index, tip_by_size.values,
marker='o', color='#ea580c', linewidth=2.5)
plt.fill_between(tip_by_size.index, tip_by_size.values,
alpha=0.1, color='#ea580c')
plt.xlabel('Party Size')
plt.ylabel('Average Tip %')
plt.title('Tip Percentage by Party Size')
plt.show()
# 3. Bill vs Tip (Scatter + Correlation)
from scipy import stats
r, p = stats.pearsonr(tips['total_bill'], tips['tip'])
print(f'Correlation: r = {r:.3f}, p = {p:.2e}')
plt.figure(figsize=(8, 5))
sns.scatterplot(data=tips, x='total_bill', y='tip',
hue='time', alpha=0.6)
sns.regplot(data=tips, x='total_bill', y='tip',
scatter=False, color='black',
line_kws={'linestyle': '--', 'alpha': 0.4})
plt.xlabel('Total Bill ($)')
plt.ylabel('Tip Amount ($)')
plt.title(f'Bill vs Tip (r = {r:.3f})')
plt.show()
# 4. Gender: Spend vs Generosity
gender_stats = tips.groupby('sex').agg(
count=('total_bill', 'count'),
avg_bill=('total_bill', 'mean'),
avg_tip_pct=('tip_pct', 'mean')
)
print(gender_stats)
plt.figure(figsize=(6, 6))
plt.pie(gender_stats['count'],
labels=['Male', 'Female'],
autopct='%1.1f%%',
colors=['#2563eb', '#ea580c'],
wedgeprops={'width': 0.4})
plt.title('Order Distribution by Gender')
plt.show()
# 5. Tip % Distribution by Smoker Status
tips['tip_pct'] = (tips['tip'] / tips['total_bill']) * 100
bins = [0, 5, 10, 15, 20, 25, 30, 100]
labels = ['0-5', '5-10', '10-15', '15-20', '20-25', '25-30', '30+']
tips['tip_bin'] = pd.cut(tips['tip_pct'], bins=bins, labels=labels)
grouped = tips.groupby(['tip_bin', 'smoker']).size().unstack(fill_value=0)
grouped.plot(kind='bar', color=['#2563eb', '#ea580c'],
width=0.8, figsize=(8, 5))
plt.xlabel('Tip % of Bill')
plt.ylabel('Frequency')
plt.legend(['Non-smoker', 'Smoker'], title='Smoker')
plt.title('Tip % Distribution by Smoker Status')
plt.show()