📖 Scenario: You are creating a simple plot to show the sales of a small shop over three days. You want to add text labels on the plot to clearly show the sales numbers on each day.
🎯 Goal: Build a plot using matplotlib that shows sales data for three days and place text labels on the plot using ax.text to display the sales numbers above each point.
📋 What You'll Learn
Create a plot with three points representing sales on three days
Use ax.text to place text labels showing sales numbers
Position the text labels slightly above each point for clarity
Print the final plot with the text labels visible
💡 Why This Matters
🌍 Real World
Adding text labels to plots helps make data easier to understand in reports and presentations.
💼 Career
Data scientists and analysts often annotate plots to highlight important values or trends for stakeholders.
Progress0 / 4 steps
1
Create the sales data and plot points
Create a list called days with values [1, 2, 3] representing three days. Create a list called sales with values [100, 150, 120] representing sales for each day. Then create a plot using fig, ax = plt.subplots() and plot the sales data as points using ax.plot(days, sales, 'o').
Matplotlib
Hint
Use plt.subplots() to create the plot and ax.plot() to plot points.
2
Set vertical offset for text labels
Create a variable called offset and set it to 5. This will be used to position the text labels slightly above the points.
Matplotlib
Hint
Just create a variable named offset and assign it the value 5.
3
Add text labels above each point
Use a for loop with variables day and sale to iterate over zip(days, sales). Inside the loop, use ax.text(day, sale + offset, str(sale)) to place the sales number as text slightly above each point.
Matplotlib
Hint
Use zip(days, sales) to loop over both lists together and ax.text() to add text.
4
Display the plot with text labels
Write plt.show() to display the plot with the points and text labels.
Matplotlib
Hint
Use plt.show() to display the plot window.
Practice
(1/5)
1. What does the ax.text function do in matplotlib?
easy
A. It draws a line between two points.
B. It places text at specified x and y coordinates on the plot.
C. It creates a new figure window.
D. It changes the background color of the plot.
Solution
Step 1: Understand the purpose of ax.text
The ax.text function is used to add text labels at specific positions on a plot using x and y coordinates.
Step 2: Compare with other options
Other options describe different matplotlib functions: creating figures, drawing lines, or changing background color, which are not what ax.text does.
Final Answer:
It places text at specified x and y coordinates on the plot. -> Option B
Quick Check:
ax.text adds text labels = C [OK]
Hint: ax.text adds text at (x, y) on the plot [OK]
Common Mistakes:
Confusing ax.text with plot or figure functions
Thinking ax.text changes plot colors
Assuming ax.text draws shapes
2. Which of the following is the correct syntax to place the text 'Hello' at coordinates (2, 3) using ax.text?
easy
A. ax.text(2, 3, 'Hello')
B. ax.text('Hello', 2, 3)
C. ax.text(x=3, y=2, text='Hello')
D. ax.text(3, 2, 'Hello')
Solution
Step 1: Recall ax.text parameter order
The correct order is ax.text(x, y, text), so x=2, y=3, text='Hello' means ax.text(2, 3, 'Hello').
Step 2: Check other options
ax.text('Hello', 2, 3) swaps text and coordinates, ax.text(x=3, y=2, text='Hello') uses wrong parameter names, ax.text(3, 2, 'Hello') swaps x and y values.
Final Answer:
ax.text(2, 3, 'Hello') -> Option A
Quick Check:
ax.text(x, y, text) = A [OK]
Hint: Remember ax.text(x, y, text) order [OK]
Common Mistakes:
Swapping x and y coordinates
Putting text before coordinates
Using wrong parameter names
3. What will the following code display on the plot?
B. ax.text requires x and y as strings, not numbers.
C. The text argument is missing.
D. fontsize must be an integer or float, not a string like 'large'.
Solution
Step 1: Check ax.text parameters
Matplotlib's ax.text requires three positional arguments: x, y, and text (s). The code provides only x=1, y=2, then a keyword argument, missing the text.
Step 2: Verify other options
Numbers for x/y are correct, fontsize='large' is valid, and plt.show() order is fine.
Final Answer:
The text argument is missing. -> Option C
Quick Check:
ax.text requires x, y, text = C [OK]
Hint: ax.text requires x, y, text positional args [OK]
Common Mistakes:
Omitting the text argument
Confusing parameter order
Calling plt.show() too early
5. You want to add a label 'Max Point' exactly above the highest point in this plot:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [2, 5, 1, 7]
fig, ax = plt.subplots()
ax.plot(x, y, marker='o')
# Add text here
plt.show()
Which code correctly places the label 0.3 units above the highest point?
hard
A. ax.text(4, 7.3, 'Max Point', va='bottom', ha='center')
B. ax.text(4, 7.3, 'Max Point', ha='center')
C. ax.text(4, 7, 'Max Point', va='top')
D. ax.text(4, 7, 'Max Point', ha='left')
Solution
Step 1: Identify highest point coordinates
The highest y value is 7 at x=4.
Step 2: Place text 0.3 units above with correct alignment
To place text above, y=7.3 is correct. Use vertical alignment va='bottom' so text bottom aligns at 7.3, placing text above the point. Horizontal alignment ha='center' centers text horizontally at x=4.
Final Answer:
ax.text(4, 7.3, 'Max Point', va='bottom', ha='center') -> Option A
Quick Check:
Text above point uses y+offset and va='bottom' = B [OK]
Hint: Use va='bottom' to place text above y coordinate [OK]
Common Mistakes:
Using va='top' places text below the coordinate
Not adding offset to y coordinate
Misaligning text horizontally
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.