Statement of Completion#4050d2f4
Visualizations with Matplotlib
easy
Understanding Amount plots
Resolution
Activities
Bar plot¶
In [1]:
import pandas as pd
import matplotlib.pyplot as plt
Introduction to bar plot¶
In [2]:
# Sample data
categories = ['Category A', 'Category B', 'Category C', 'Category D']
sales = [120, 210, 180, 250]
In [3]:
# Initialize the figure and axes
fig, ax = plt.subplots(figsize=(8, 6))
# Create a basic vertical bar plot
ax.bar(categories, sales, color='skyblue')
# Customize the plot
ax.set_xlabel('Product Categories')
ax.set_ylabel('Number of Products Sold')
ax.set_title('Product Sales by Category')
ax.set_xticklabels(categories, rotation=45)
# Display the plot
plt.show()
/tmp/ipykernel_17/1216360248.py:11: UserWarning: set_ticklabels() should only be used with a fixed number of ticks, i.e. after set_ticks() or using a FixedLocator. ax.set_xticklabels(categories, rotation=45)
Example¶
In [4]:
df=pd.read_csv('tips.csv')
df.head()
Out[4]:
total_bill | tip | sex | smoker | day | time | size | |
---|---|---|---|---|---|---|---|
0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
Visualize the average tip amount by gender (Male vs. Female)¶
In [5]:
# Calculate the average tip amount by gender
avg_tip_by_gender = df.groupby("sex")["tip"].mean().reset_index()
In [6]:
# Create a figure and a set of subplots
fig, ax = plt.subplots(figsize=(8, 6))
# Create a bar plot to compare average tips by gender using Matplotlib on ax
ax.bar(avg_tip_by_gender["sex"], avg_tip_by_gender["tip"], color=["skyblue", "salmon"])
ax.set_xlabel("Gender")
ax.set_ylabel("Average Tip Amount")
ax.set_title("Average Tips by Gender")
Out[6]:
Text(0.5, 1.0, 'Average Tips by Gender')
Compare the total bill amounts for different days of the week¶
In [7]:
# Prepare the data
days = df["day"].unique()
total_bills_by_day = df.groupby("day")["total_bill"].sum()
In [8]:
# Create a figure and axis object
fig, ax = plt.subplots(figsize=(10, 6))
# Plot the data
ax.bar(days, total_bills_by_day, color="skyblue")
# Set labels and title
ax.set_xlabel("Day of the Week")
ax.set_ylabel("Total Bill Amount")
ax.set_title("Total Bill Amounts by Day of the Week")
Out[8]:
Text(0.5, 1.0, 'Total Bill Amounts by Day of the Week')
Explore the distribution of party sizes¶
In [9]:
# Create a figure and an axes object
fig, ax = plt.subplots(figsize=(8, 6))
# Generate the bar plot on the axes object
party_counts = df["size"].value_counts()
ax.bar(party_counts.index, party_counts, color="skyblue")
# Set the labels and title of the plot
ax.set_xlabel("Party Size")
ax.set_ylabel("Count")
ax.set_title("Distribution of Party Sizes")
Out[9]:
Text(0.5, 1.0, 'Distribution of Party Sizes')
Investigate the preference for mealtime (Lunch vs. Dinner) in terms of tips:¶
In [10]:
# Prepare data for a bar plot to compare average tips for lunch and dinner
fig, ax = plt.subplots(figsize=(8, 6))
meal_times = df["time"].unique()
average_tips = df.groupby("time")["tip"].mean()
colors = ["skyblue", "salmon"]
# Create bar plot
ax.bar(meal_times, average_tips, color=colors)
ax.set_xlabel("Mealtime")
ax.set_ylabel("Average Tip Amount")
ax.set_title("Average Tips by Mealtime (Lunch vs. Dinner)")
Out[10]:
Text(0.5, 1.0, 'Average Tips by Mealtime (Lunch vs. Dinner)')
Stacked bar plots¶
Here's the code to generate and introduce a stacked bar plot for the "Tips" dataset:
In [12]:
df.groupby(["day", "time"])["total_bill"].sum().unstack()
Out[12]:
time | Dinner | Lunch |
---|---|---|
day | ||
Fri | 235.96 | 89.92 |
Sat | 1778.40 | NaN |
Sun | 1627.16 | NaN |
Thur | 18.78 | 1077.55 |
In [13]:
# Calculate the total bill amount by day and time (Lunch vs. Dinner)
total_bill_by_day_time = df.groupby(["day", "time"])["total_bill"].sum().unstack().reset_index()
# Initialize the figure and axes for a more flexible plotting
fig, ax = plt.subplots(figsize=(10, 6))
# Extract the days and total bill amounts for Lunch and Dinner
days = total_bill_by_day_time["day"]
lunch_bills = total_bill_by_day_time["Lunch"]
dinner_bills = total_bill_by_day_time["Dinner"]
# Create the stacked bars for Lunch and Dinner
ax.bar(days, lunch_bills, label="Lunch", color="skyblue")
ax.bar(days, dinner_bills, bottom=lunch_bills, label="Dinner", color="salmon")
ax.set_xlabel("Day of the Week")
ax.set_ylabel("Total Bill Amount")
ax.set_title("Composition of Total Bills by Day and Time")
ax.legend(title="Mealtime", loc="upper right")
Out[13]:
<matplotlib.legend.Legend at 0x7c5f68e7b4d0>
Activity 1. Draw four horizontal bars with different lengths, labels, and colors using Matplotlib.¶
In [14]:
bar_lengths = [3, 4, 5, 6] # Adjust the lengths as needed
bar_labels = ['Bar 1', 'Bar 2', 'Bar 3', 'Bar 4']
bar_colors = ['skyblue', 'salmon', 'lightgreen', 'lightcoral']
In [19]:
# Create a horizontal bar plot
fig, ax = plt.subplots(figsize=(8, 6))
# Create the horizontal bars
ax.barh(bar_labels, bar_lengths, color =bar_colors)
# Add labels and title
ax.set_title('Horizontal Bar Plot')
ax.set_xlabel('Length')
ax.set_ylabel('Bars')
Out[19]:
Text(0, 0.5, 'Bars')