0
0
Matplotlibdata~20 mins

Tick marks and tick labels in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tick Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of custom tick labels on x-axis
What will be the x-axis tick labels shown by this code snippet?
Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([10, 20, 30, 40])
ax.set_xticks([0, 1, 2, 3])
ax.set_xticklabels(['A', 'B', 'C', 'D'])
plt.show()
ATicks at positions 10,20,30,40 labeled as A,B,C,D
BNo ticks shown on x-axis
CTicks at positions 0,1,2,3 labeled as 0,1,2,3
DTicks at positions 0,1,2,3 labeled as A,B,C,D
Attempts:
2 left
💡 Hint
Remember set_xticks sets positions, set_xticklabels sets labels for those positions.
data_output
intermediate
2:00remaining
Number of major ticks on y-axis after setting limits
After running this code, how many major ticks will appear on the y-axis?
Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4, 5])
ax.set_ylim(0, 10)
plt.show()

num_ticks = len(ax.get_yticks())
print(num_ticks)
A7
B5
C11
D6
Attempts:
2 left
💡 Hint
Default major ticks are spaced evenly within the limits.
visualization
advanced
2:00remaining
Effect of rotating tick labels
Which option shows the correct effect of rotating x-axis tick labels by 45 degrees?
Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4])
ax.set_xticks([0, 1, 2, 3])
ax.set_xticklabels(['Jan', 'Feb', 'Mar', 'Apr'], rotation=45)
plt.show()
ATick labels are horizontal and overlap
BTick labels are rotated 45 degrees clockwise
CTick labels are rotated 90 degrees vertically
DTick labels are hidden
Attempts:
2 left
💡 Hint
Check the rotation parameter in set_xticklabels.
🧠 Conceptual
advanced
2:00remaining
Understanding difference between major and minor ticks
Which statement correctly describes the difference between major and minor ticks in matplotlib?
AMinor ticks appear only on the y-axis; major ticks only on the x-axis
BMajor and minor ticks are the same and always labeled
CMajor ticks are labeled by default; minor ticks are smaller and unlabeled unless specified
DMinor ticks are labeled by default; major ticks are unlabeled
Attempts:
2 left
💡 Hint
Think about which ticks usually have labels.
🔧 Debug
expert
2:00remaining
Identify error in setting tick labels
What error will this code produce when run?
Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3])
ax.set_xticks([0, 1, 2])
ax.set_xticklabels(['One', 'Two'])
plt.show()
AValueError: The number of labels must match the number of ticks
BTypeError: set_xticklabels expects a list of numbers
CNo error, plot shows with default labels
DIndexError: list index out of range
Attempts:
2 left
💡 Hint
Check if the number of labels matches the number of tick positions.