Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 08

Axis Limits and Ticks

Zoom into part of a chart with xlim and ylim, choose which tick numbers appear with xticks and yticks, and see how limits can mislead.

Intermediate25 min
Axis Limits and Ticks hero Module 25 Lesson 8Axis Limits and Ticks hero Module 25 Lesson 8

Matplotlib picks an axis range that fits your data with a little room to spare. Usually that is right. When it is not, xlim and ylim set the range yourself, and xticks and yticks decide which numbers get printed. Both are useful, and both can be used to mislead, so use them deliberately.

A line chart with the window kept by xlim highlightedA line chart with the window kept by xlim highlighted

Setting the range

plt.xlim(low, high) and plt.ylim(low, high) set the visible range. Called with no arguments they report the current range instead, which is handy when you want to nudge one end and leave the other alone.

python
import matplotlib.pyplot as plt

hours = list(range(0, 24))
temperature = [
    11, 10, 10, 9, 9, 10, 12, 14, 17, 19, 21, 23,
    24, 25, 25, 24, 22, 20, 18, 16, 15, 13, 12, 11,
]

plt.plot(hours, temperature)
print("range Matplotlib chose:", plt.xlim())

plt.xlim(6, 18)
print("range after xlim:", plt.xlim())
plt.title("Daylight hours only")
plt.show()

Output:

text
range Matplotlib chose: (-1.1500000000000001, 24.15)
range after xlim: (6.0, 18.0)

Starting the value axis at zero

A value axis that starts part way up exaggerates every difference. The same four numbers can look like a crisis or a rounding error depending on where the axis begins. For bar charts, starting at zero is not a preference, it is a requirement, because the bar length is the message.

python
import matplotlib.pyplot as plt

quarters = ["Q1", "Q2", "Q3", "Q4"]
score = [81, 83, 82, 85]

plt.plot(quarters, score, marker="o")
plt.ylim(80, 86)
plt.title("Looks dramatic")
plt.show()

plt.plot(quarters, score, marker="o")
plt.ylim(0, 100)
plt.title("The honest version")
plt.show()

print("actual spread:", max(score) - min(score), "points")

Output:

text
actual spread: 4 points

Four points of movement on a hundred-point scale. The first chart makes that look like a transformation.

Choosing the ticks

plt.xticks(values) prints a tick only at the positions you list, and a second argument gives each one a label of your own. rotation turns crowded labels so they stop overlapping.

python
import matplotlib.pyplot as plt

hours = list(range(0, 24))
temperature = [
    11, 10, 10, 9, 9, 10, 12, 14, 17, 19, 21, 23,
    24, 25, 25, 24, 22, 20, 18, 16, 15, 13, 12, 11,
]

plt.plot(hours, temperature)
plt.xticks([0, 6, 12, 18, 23], ["midnight", "6am", "noon", "6pm", "11pm"], rotation=30)
plt.yticks([0, 10, 20, 30])
plt.xlabel("time of day")
plt.ylabel("degrees")
plt.show()

print("warmest hour:", temperature.index(max(temperature)))

Output:

text
warmest hour: 13

The same calls on an axes object

When you hold the axes in a variable, the names change slightly: set_xlim, set_ylim, set_xticks, and set_xticklabels. The behaviour is identical, and you need these once a figure has more than one plot.

python
import matplotlib.pyplot as plt

hours = list(range(0, 24))
temperature = [
    11, 10, 10, 9, 9, 10, 12, 14, 17, 19, 21, 23,
    24, 25, 25, 24, 22, 20, 18, 16, 15, 13, 12, 11,
]

figure, area = plt.subplots(figsize=(8, 4))
area.plot(hours, temperature)
area.set_xlim(0, 23)
area.set_ylim(0, 30)
area.set_xticks([0, 6, 12, 18])
area.set_xticklabels(["00:00", "06:00", "12:00", "18:00"])
print(area.get_ylim())
plt.show()

Output:

text
(0.0, 30.0)

Key takeaways

  • xlim and ylim set the visible range, and report it when called empty

  • Cutting the value axis exaggerates small differences

  • Bar charts must start at zero, because the length is the message

  • xticks chooses the positions, and a second argument labels them

  • Next: Several Plots in One Figure

  • Previous: Grid Lines and Fonts

Loading playground…

Check your understanding

Axis Limits and Ticks Quiz

What does plt.xlim(2, 8) do?

1 / 10