0
0
Matplotlibdata~30 mins

Major and minor ticks in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Major and Minor Ticks in Matplotlib
📖 Scenario: You are creating a simple line chart to show the sales trend over 10 days. To make the chart easier to read, you want to add both major and minor ticks on the x-axis.
🎯 Goal: Build a matplotlib line plot with major ticks every 2 days and minor ticks every day on the x-axis.
📋 What You'll Learn
Create a list called days with values from 1 to 10.
Create a list called sales with these exact values: 5, 7, 9, 6, 8, 10, 7, 9, 11, 8.
Create a plot with days on the x-axis and sales on the y-axis.
Set major ticks on the x-axis at every 2 days using MultipleLocator(2).
Set minor ticks on the x-axis at every 1 day using MultipleLocator(1).
Display the plot.
💡 Why This Matters
🌍 Real World
In real life, clear charts with well-placed ticks help people understand data trends easily, like sales over time or temperature changes.
💼 Career
Data scientists and analysts often customize plots to make reports and dashboards more readable and professional.
Progress0 / 4 steps
1
Create the data lists
Create a list called days with values from 1 to 10, and a list called sales with these exact values: 5, 7, 9, 6, 8, 10, 7, 9, 11, 8.
Matplotlib
Need a hint?

Use square brackets to create lists. For days, list numbers 1 through 10. For sales, use the exact numbers given.

2
Import matplotlib and set up the plot
Import matplotlib.pyplot as plt and import MultipleLocator from matplotlib.ticker. Then create a plot with days on the x-axis and sales on the y-axis using plt.plot(days, sales).
Matplotlib
Need a hint?

Use import matplotlib.pyplot as plt and from matplotlib.ticker import MultipleLocator. Then call plt.plot(days, sales) to create the line chart.

3
Add major and minor ticks on the x-axis
Use plt.gca() to get the current axes and assign it to ax. Then set major ticks on the x-axis every 2 days using ax.xaxis.set_major_locator(MultipleLocator(2)). Set minor ticks on the x-axis every 1 day using ax.xaxis.set_minor_locator(MultipleLocator(1)).
Matplotlib
Need a hint?

Use plt.gca() to get the axes. Then use ax.xaxis.set_major_locator(MultipleLocator(2)) and ax.xaxis.set_minor_locator(MultipleLocator(1)) to set ticks.

4
Display the plot
Use plt.show() to display the plot with major and minor ticks on the x-axis.
Matplotlib
Need a hint?

Call plt.show() to open the plot window and see the chart with ticks.