5.5.3. Homework#
Use this assignment to practice the chapter’s concepts and coding skills in runnable Python code.
Sign in from the account menu before you start working on the assignment.
Write and run your code directly in the coding cells.
Multiple submissions are allowed. The highest score is kept.
1. Chart Choice
A manager comparing revenue across regions would usually start with a comparison chart such as a bar chart.
2. Line Charts
A line chart is usually appropriate when the business question focuses on change over time.
3. Scatterplots
A scatterplot is mainly used to show the parts of a whole, so it is the default choice for market share percentages.
4. Outliers
A chart can reveal an unusual sales value that should be investigated before a report is finalized.
5. Labels
Removing axis labels usually makes a chart easier for a business audience to interpret.
### 6. Stacked Bar Chart
# Show each quarter's total units and how the two products contribute.
# 1. Draw a bar chart of both product columns by quarter, stacked:
# df.plot.bar(x="quarter", stacked=True).
# 2. Set the title "Units by Quarter".
import pandas as pd
df = pd.DataFrame({
"quarter": ["Q1", "Q2", "Q3", "Q4"],
"Product A": [30, 35, 32, 40],
"Product B": [20, 25, 28, 30],
})
### Your code starts here.
### Your code ends here.
### 7. Horizontal Bar Chart
# Product names are long, so list them down the side.
# 1. Draw a horizontal bar chart with df.plot.barh(x="product", y="units").
# 2. Turn off the legend and set the title "Units Sold by Product".
import pandas as pd
df = pd.DataFrame({
"product": ["Wireless Mouse", "USB-C Hub", "Laptop Stand", "Webcam"],
"units": [120, 95, 80, 60],
})
### Your code starts here.
### Your code ends here.
### 8. Price and Demand
# Check how demand changes with price when many points overlap.
# 1. Draw a scatter plot with df.plot.scatter(x="price", y="demand").
# 2. Make the points half transparent (alpha=0.5).
# 3. Set the title "Price vs Demand".
import pandas as pd
df = pd.DataFrame({
"price": [5, 6, 7, 8, 9, 10, 11, 12],
"demand": [200, 185, 170, 150, 140, 120, 110, 95],
})
### Your code starts here.
### Your code ends here.
### 9. Order Value Histogram
# Show the distribution of order values in a small report figure.
# 1. Draw a histogram of order_value with .plot.hist().
# 2. Use 6 bins, a 6 by 3 inch figure (figsize=(6, 3)), and alpha=0.7.
# 3. Set the title "Order Values".
import pandas as pd
df = pd.DataFrame({"order_value": [45, 52, 38, 61, 75, 49, 58, 66, 40, 55, 70, 48]})
### Your code starts here.
### Your code ends here.
### 10. Honest Bar Scale
# Satisfaction scores are close together. A bar chart should start at zero
# so the differences are not exaggerated.
# 1. Draw a bar chart with df.plot.bar(x="region", y="satisfaction").
# 2. Set the y-axis range from 0 to 100 (ylim=(0, 100)) and turn off the legend.
# 3. Set the title "Customer Satisfaction by Region".
import pandas as pd
df = pd.DataFrame({
"region": ["East", "West", "South", "North"],
"satisfaction": [88, 91, 85, 93],
})
### Your code starts here.
### Your code ends here.