Bird
Raised Fist0
Matplotlibdata~20 mins

Title and axis labels in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Titles and Labels
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:30remaining
What is the title of the plot?
Look at the code below. What will be the title shown on the plot?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.title('Sales Over Time')
plt.xlabel('Month')
plt.ylabel('Revenue')
plt.show()
ASales Data
BSales Over Time
CMonth
DRevenue
Attempts:
2 left
💡 Hint
The title is set by plt.title() function.
❓ Predict Output
intermediate
1:30remaining
What is the label of the x-axis?
What label will appear on the x-axis in the plot below?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([10, 20, 30], [1, 4, 9])
plt.title('Growth Chart')
plt.xlabel('Time (years)')
plt.ylabel('Value')
plt.show()
AGrowth Chart
BValue
CTime (years)
DYears
Attempts:
2 left
💡 Hint
The x-axis label is set by plt.xlabel().
🔧 Debug
advanced
2:00remaining
Why does this code not show the y-axis label?
The code below is supposed to show a y-axis label, but it does not appear. What is the problem?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [3, 2, 1])
plt.title('Sample Plot')
plt.xlabel('X Axis')
plt.ylabel('')
plt.show()
AThe y-axis label is empty, so nothing shows.
Bplt.ylabel() is missing parentheses.
Cplt.title() must be called after plt.ylabel().
DThe plot() function must include label parameter.
Attempts:
2 left
💡 Hint
Check the argument passed to plt.ylabel().
❓ data_output
advanced
2:00remaining
What are the axis labels after this code?
After running the code below, what are the labels of the x-axis and y-axis?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([5, 10, 15], [2, 4, 6])
plt.xlabel('Distance (km)')
plt.ylabel('Speed (km/h)')
plt.xlabel('Time (seconds)')
plt.show()
Ax-axis: 'Time (seconds)', y-axis: 'Speed (km/h)'
Bx-axis: 'Distance (km)', y-axis: 'Speed (km/h)'
Cx-axis: 'Distance (km)', y-axis: ''
Dx-axis: '', y-axis: 'Speed (km/h)'
Attempts:
2 left
💡 Hint
The last plt.xlabel() call overwrites the previous one.
🧠 Conceptual
expert
1:30remaining
Why is it important to label axes and add titles in plots?
Choose the best reason why adding titles and axis labels is important in data visualization.
AThey allow the plot to run faster in Python.
BThey make the plot look colorful and attractive.
CThey increase the file size of the plot image.
DThey help viewers understand what the data and axes represent.
Attempts:
2 left
💡 Hint
Think about how someone else reads your plot.

Practice

(1/5)
1. Which matplotlib function is used to add a title to a plot?
easy
A. plt.axis()
B. plt.label()
C. plt.legend()
D. plt.title()

Solution

  1. Step 1: Understand the purpose of plt.title()

    This function adds a title text at the top of the plot, describing what the plot shows.
  2. Step 2: Compare with other functions

    plt.label() does not exist, plt.legend() adds a legend, and plt.axis() controls axis limits, so they don't add a title.
  3. Final Answer:

    plt.title() -> Option D
  4. Quick Check:

    Title function = plt.title() [OK]
Hint: Title is always set with plt.title() [OK]
Common Mistakes:
  • Using plt.label() instead of plt.title()
  • Confusing legend with title
  • Trying plt.axis() for title
2. Which of the following is the correct syntax to label the x-axis as 'Time (seconds)' in matplotlib?
easy
A. plt.xlabel('Time (seconds)')
B. plt.xaxis('Time (seconds)')
C. plt.label('x', 'Time (seconds)')
D. plt.axislabel('Time (seconds)')

Solution

  1. Step 1: Recall the function to label x-axis

    The correct function is plt.xlabel() which takes the label text as argument.
  2. Step 2: Check other options for syntax errors

    plt.xaxis(), plt.label(), and plt.axislabel() are not valid matplotlib functions for axis labeling.
  3. Final Answer:

    plt.xlabel('Time (seconds)') -> Option A
  4. Quick Check:

    X-axis label = plt.xlabel() [OK]
Hint: Use plt.xlabel() for x-axis label text [OK]
Common Mistakes:
  • Using plt.xaxis() which doesn't exist
  • Trying plt.label() for axis
  • Confusing axis limits with labels
3. What will be the output of the following code?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.title('Sales Over Time')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.show()
medium
A. A plot with title but no axis labels
B. A plot with no title and no axis labels
C. A plot with title 'Sales Over Time', x-axis labeled 'Month', y-axis labeled 'Sales'
D. A plot with axis labels but no title

Solution

  1. Step 1: Analyze the code lines for title and labels

    The code uses plt.title('Sales Over Time') to add a title, plt.xlabel('Month') for x-axis label, and plt.ylabel('Sales') for y-axis label.
  2. Step 2: Understand the plot display

    The plot will show the line graph with all these labels visible as specified.
  3. Final Answer:

    A plot with title 'Sales Over Time', x-axis labeled 'Month', y-axis labeled 'Sales' -> Option C
  4. Quick Check:

    Title and axis labels set correctly [OK]
Hint: Check all label functions called before plt.show() [OK]
Common Mistakes:
  • Forgetting plt.title() call
  • Mixing up xlabel and ylabel
  • Not calling plt.show() to display
4. Identify the error in this code snippet that tries to add a title and axis labels:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [3, 2, 1])
plt.title = 'My Plot'
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.show()
medium
A. Using assignment operator '=' instead of function call for plt.title
B. Missing plt.show() call
C. Incorrect order of xlabel and ylabel calls
D. plt.plot() arguments are invalid

Solution

  1. Step 1: Check how title is set

    The code uses plt.title = 'My Plot' which assigns a string to the function name, overwriting it instead of calling it.
  2. Step 2: Understand correct usage

    The correct way is to call the function: plt.title('My Plot') to set the title.
  3. Final Answer:

    Using assignment operator '=' instead of function call for plt.title -> Option A
  4. Quick Check:

    Title must be set by calling plt.title() [OK]
Hint: Use parentheses () to call plt.title() function [OK]
Common Mistakes:
  • Assigning string to plt.title instead of calling
  • Forgetting plt.show()
  • Swapping xlabel and ylabel calls
5. You want to create a plot with the title 'Temperature Trends', x-axis labeled 'Day', and y-axis labeled 'Temperature (°C)'. Which code snippet correctly achieves this?
hard
A. plt.plot(days, temps) plt.title = 'Temperature Trends' plt.xlabel = 'Day' plt.ylabel = 'Temperature (°C)' plt.show()
B. plt.plot(days, temps) plt.title('Temperature Trends') plt.xlabel('Day') plt.ylabel('Temperature (°C)') plt.show()
C. plt.plot(days, temps) plt.set_title('Temperature Trends') plt.set_xlabel('Day') plt.set_ylabel('Temperature (°C)') plt.show()
D. plt.plot(days, temps) plt.title('Temperature Trends') plt.xaxis('Day') plt.yaxis('Temperature (°C)') plt.show()

Solution

  1. Step 1: Verify correct function calls for title and labels

    plt.plot(days, temps) plt.title('Temperature Trends') plt.xlabel('Day') plt.ylabel('Temperature (°C)') plt.show() uses plt.title(), plt.xlabel(), and plt.ylabel() correctly with parentheses and proper arguments.
  2. Step 2: Check other options for errors

    plt.plot(days, temps) plt.title = 'Temperature Trends' plt.xlabel = 'Day' plt.ylabel = 'Temperature (°C)' plt.show() incorrectly assigns strings to functions, plt.plot(days, temps) plt.set_title('Temperature Trends') plt.set_xlabel('Day') plt.set_ylabel('Temperature (°C)') plt.show() uses non-existent set_title functions, and plt.plot(days, temps) plt.title('Temperature Trends') plt.xaxis('Day') plt.yaxis('Temperature (°C)') plt.show() uses invalid xaxis and yaxis functions.
  3. Final Answer:

    plt.plot(days, temps) plt.title('Temperature Trends') plt.xlabel('Day') plt.ylabel('Temperature (°C)') plt.show() -> Option B
  4. Quick Check:

    Use plt.title(), plt.xlabel(), plt.ylabel() with parentheses [OK]
Hint: Always call plt.title(), plt.xlabel(), plt.ylabel() as functions [OK]
Common Mistakes:
  • Assigning labels instead of calling functions
  • Using non-existent set_title() methods
  • Using plt.xaxis() instead of plt.xlabel()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard