Seaborn Exercises
Problem 1: Load Seaborn’s built-in tips dataset (which contains restaurant tipping data). Create a histogram to visualize the distribution of the total_bill amounts to see what the most common bill sizes are.
seaborn-exercises.py
import seaborn as sns
import matplotlib.pyplot as plt
# 1. Load the built-in dataset into a pandas DataFrame
tips = sns.load_dataset("tips")
# 2. Create the histogram
sns.histplot(data=tips, x="total_bill")
# 3. Display the plot
plt.show()
Problem 2: Using the same tips dataset, create a visual that compares the distribution of the total_bill across different days of the week.
seaborn-exercises.py
import seaborn as sns
import matplotlib.pyplot as plt
# 1. Load the data
tips = sns.load_dataset("tips")
# 2. Create a boxplot mapping 'day' to X and 'total_bill' to Y
sns.boxplot(data=tips, x="day", y="total_bill")
# 3. Add a title (using standard Matplotlib commands)
plt.title("Total Bill Distribution by Day")
# 4. Display the plot
plt.show()
Problem 3: Create a scatterplot to see the relationship between the total_bill and the tip amount. To make it more insightful, color-code the data points based on whether the table section was a smoker or non-smoker section.
seaborn-exercises.py
import seaborn as sns
import matplotlib.pyplot as plt
# 1. Load the data
tips = sns.load_dataset("tips")
# 2. Create a scatterplot and use 'hue' to color by a category
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="smoker")
# 3. Display the plot
plt.show()