Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Statistical plot enhancements
📖 Scenario: You work as a data analyst for a small company. You have collected sales data for different products over several months. Your manager wants you to create a clear and informative bar chart that shows the sales numbers and highlights the highest sales month.
🎯 Goal: Build a bar chart using matplotlib that shows monthly sales data. You will add enhancements like colors, labels, and a title to make the chart easy to understand and visually appealing.
📋 What You'll Learn
Create a dictionary with monthly sales data
Set a variable for the month with the highest sales
Use a bar chart to plot the sales data
Highlight the highest sales month with a different color
Add labels for the x-axis, y-axis, and a chart title
Display the final plot
💡 Why This Matters
🌍 Real World
Data analysts often need to visualize sales or performance data clearly to help managers make decisions. Highlighting important data points like the highest sales month makes reports more useful.
💼 Career
Knowing how to create and enhance plots with matplotlib is a key skill for data scientists and analysts to communicate insights effectively.
Progress0 / 4 steps
1
Create monthly sales data
Create a dictionary called monthly_sales with these exact entries: 'January': 150, 'February': 200, 'March': 180, 'April': 220, 'May': 170.
Matplotlib
Hint
Use curly braces {} to create a dictionary. Separate each month and sales number with a colon.
2
Identify the highest sales month
Create a variable called highest_month that stores the month with the highest sales from the monthly_sales dictionary.
Matplotlib
Hint
Use the max() function with the key=monthly_sales.get argument to find the month with the highest sales.
3
Create a bar chart with color highlights
Use matplotlib.pyplot to create a bar chart of monthly_sales. Use a list comprehension to create a list of colors where the bar for highest_month is 'orange' and all others are 'blue'. Use this color list in the color parameter of plt.bar().
Matplotlib
Hint
Use a list comprehension to assign colors. Then pass the colors list to the color argument in plt.bar().
4
Add labels and display the plot
Add an x-axis label 'Month', a y-axis label 'Sales', and a title 'Monthly Sales Data' to the plot using plt.xlabel(), plt.ylabel(), and plt.title(). Then display the plot using plt.show().
Matplotlib
Hint
Use plt.xlabel(), plt.ylabel(), and plt.title() to add labels and title. Use plt.show() to display the plot.
Practice
(1/5)
1. What is the main purpose of adding a legend to a matplotlib plot?
easy
A. To explain what different colors or markers represent
B. To change the plot's background color
C. To save the plot as an image file
D. To remove grid lines from the plot
Solution
Step 1: Understand what a legend does
A legend shows labels for different plot elements like colors or markers.
Step 2: Match legend purpose to options
Only To explain what different colors or markers represent describes explaining plot elements, which is the legend's role.
Final Answer:
To explain what different colors or markers represent -> Option A
Quick Check:
Legend = Explain plot elements [OK]
Hint: Legend explains plot symbols and colors [OK]
Common Mistakes:
Confusing legend with grid or background settings
Thinking legend saves the plot
Assuming legend removes plot elements
2. Which of the following is the correct way to add a title to a matplotlib plot?
easy
A. plt.set_title('My Plot')
B. plt.add_title('My Plot')
C. plt.title('My Plot')
D. plt.plot_title('My Plot')
Solution
Step 1: Recall matplotlib title syntax
The correct function to add a title is plt.title().
Step 2: Check options for correct function name
Only plt.title('My Plot') uses plt.title('My Plot'), which is correct syntax.
Final Answer:
plt.title('My Plot') -> Option C
Quick Check:
Title function = plt.title() [OK]
Hint: Use plt.title() to add plot titles [OK]
Common Mistakes:
Using incorrect function names like set_title or add_title
The plot call has only one list, so it treats it as y-values with x as indices 0,1,2.
Step 2: Understand matplotlib behavior
This is valid syntax; it plots y-values against default x-values. So no error here.
Step 3: Re-examine options carefully
The plot function is missing y-values says missing y-values, but y-values are given. The legend function is called before plot is wrong order. The label parameter is invalid in plot label is valid. There is no error; the code runs correctly says no error.
Final Answer:
There is no error; the code runs correctly -> Option D
Quick Check:
Code runs fine with legend after plot [OK]
Hint: Check if plot syntax matches matplotlib docs [OK]
Common Mistakes:
Assuming single list plot is invalid
Thinking legend must come before plot
Believing label is not accepted in plot
5. You want to create a scatter plot with blue triangles, add grid lines, and label axes as 'Height' and 'Weight'. Which code snippet correctly does this?
hard
A. plt.scatter(x, y, marker='s', color='red')
plt.grid(True)
plt.xlabel('Weight')
plt.ylabel('Height')
B. plt.scatter(x, y, marker='^', color='blue')
plt.grid(True)
plt.xlabel('Height')
plt.ylabel('Weight')
C. plt.plot(x, y, marker='o', color='green')
plt.grid(False)
plt.xlabel('Weight')
plt.ylabel('Height')
D. plt.plot(x, y, marker='^', color='blue')
plt.grid(True)
plt.xlabel('Height')
plt.ylabel('Weight')
Solution
Step 1: Identify scatter plot with blue triangles
Use plt.scatter() with marker='^' and color='blue'.
Step 2: Check grid and axis labels
Grid must be enabled with plt.grid(True), and axes labeled 'Height' and 'Weight' correctly.
Step 3: Match code snippet to requirements
plt.scatter(x, y, marker='^', color='blue')
plt.grid(True)
plt.xlabel('Height')
plt.ylabel('Weight') matches all requirements exactly.
Final Answer:
plt.scatter(x, y, marker='^', color='blue')
plt.grid(True)
plt.xlabel('Height')
plt.ylabel('Weight') -> Option B
Quick Check:
Scatter + blue triangles + grid + correct labels = plt.scatter(x, y, marker='^', color='blue')
plt.grid(True)
plt.xlabel('Height')
plt.ylabel('Weight') [OK]