0
0
Matplotlibdata~15 mins

Text placement with ax.text in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Text placement with ax.text
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use plt.show() to display the plot window.