📖 Scenario: You are analyzing sales data for a small store. You want to create a simple line chart to show sales over days. Proper axis formatting will help make the chart clear and easy to understand.
🎯 Goal: Build a line chart using matplotlib that shows sales over days with clear axis labels and formatted ticks.
📋 What You'll Learn
Create a list called days with values 1 to 7 representing days of the week
Create a list called sales with exact values: 150, 200, 170, 220, 180, 210, 190
Create a matplotlib line plot using days and sales
Set the x-axis label to 'Day of Week'
Set the y-axis label to 'Sales ($)'
Format the y-axis ticks to show dollar sign and no decimals
Display the plot
💡 Why This Matters
🌍 Real World
Axis formatting helps make charts easy to read and understand in reports and presentations.
💼 Career
Data scientists and analysts use axis formatting to communicate data insights clearly to others.
Progress0 / 4 steps
1
Create the sales data lists
Create a list called days with values 1, 2, 3, 4, 5, 6, 7 and a list called sales with values 150, 200, 170, 220, 180, 210, 190.
Matplotlib
Hint
Use square brackets to create lists with the exact numbers given.
2
Import matplotlib and create the plot
Import matplotlib.pyplot as plt. Use plt.plot(days, sales) to create a line plot.
Matplotlib
Hint
Use import matplotlib.pyplot as plt and then plt.plot() with the two lists.
3
Add axis labels and format y-axis ticks
Use plt.xlabel('Day of Week') and plt.ylabel('Sales ($)') to label the axes. Then import matplotlib.ticker and use plt.gca().yaxis.set_major_formatter(matplotlib.ticker.StrMethodFormatter('${x:,.0f}')) to format y-axis ticks with dollar sign and no decimals.
Matplotlib
Hint
Use plt.xlabel() and plt.ylabel() for labels. Use matplotlib.ticker.StrMethodFormatter to format y-axis ticks.
4
Display the plot
Use plt.show() to display the plot with the formatted axes.
Matplotlib
Hint
Use plt.show() to display the plot window.
Practice
(1/5)
1. Why is axis formatting important when creating charts with matplotlib?
easy
A. It adds animation effects to the chart.
B. It changes the color of the chart background.
C. It automatically saves the chart to a file.
D. It helps make the chart easier to understand by labeling and scaling axes properly.
Solution
Step 1: Understand the role of axis formatting
Axis formatting controls labels, ticks, and limits on the axes, which helps viewers read the chart easily.
Step 2: Identify the effect on chart clarity
Proper axis formatting makes the data story clear by showing correct scales and labels.
Final Answer:
It helps make the chart easier to understand by labeling and scaling axes properly. -> Option D
Quick Check:
Axis formatting = clarity [OK]
Hint: Axis formatting improves chart clarity and understanding [OK]
B. The code uses assignment instead of calling plt.ylim() function.
C. plt.show() is missing and causes no output.
D. There is no error; the code runs correctly.
Solution
Step 1: Check how y-axis limits are set
In matplotlib, plt.ylim() is a function to set y-axis limits, not an attribute to assign.
Step 2: Identify the mistake in code
The code uses plt.ylim = (0, 10) which assigns a tuple instead of calling the function plt.ylim(0, 10).
Final Answer:
The code uses assignment instead of calling plt.ylim() function. -> Option B
Quick Check:
Use plt.ylim() function, not assignment [OK]
Hint: Use plt.ylim() as a function, not assignment [OK]
Common Mistakes:
Assigning tuple to plt.ylim instead of calling it
Forgetting plt.show() to display plot
Confusing data errors with axis formatting errors
5. You want to create a plot where the x-axis shows dates but only every Monday is labeled to avoid clutter. Which approach best formats the axis in matplotlib?
hard
A. Use matplotlib.dates.WeekdayLocator to set ticks on Mondays and format labels with DateFormatter.
B. Rotate all date labels by 90 degrees without changing ticks.
C. Manually set x-axis limits to only show Mondays.
D. Use plt.xticks() with all dates as labels.
Solution
Step 1: Understand the problem of cluttered date labels
Showing all dates can clutter the axis; selecting specific days like Mondays reduces clutter.
Step 2: Identify matplotlib tools for date axis formatting
WeekdayLocator can set ticks on specific weekdays, and DateFormatter formats the tick labels nicely.
Step 3: Evaluate options
The approach using matplotlib.dates.WeekdayLocator to set ticks on Mondays and format labels with DateFormatter correctly reduces clutter; others either show all dates, only rotate labels without reducing ticks, or limit the axis range.
Final Answer:
Use matplotlib.dates.WeekdayLocator to set ticks on Mondays and format labels with DateFormatter. -> Option A
Quick Check:
Use WeekdayLocator + DateFormatter for clean date axes [OK]
Hint: Use WeekdayLocator and DateFormatter for selective date ticks [OK]
Common Mistakes:
Labeling all dates causing clutter
Only rotating labels without reducing ticks
Trying to limit axis range instead of ticks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.