Bird
Raised Fist0
Matplotlibdata~20 mins

Label, title, and axis names in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Label Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the title of the plot?
Consider this code that creates a simple plot with matplotlib. 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
BRevenue
CSales Over Time
DMonth
Attempts:
2 left
💡 Hint
Look for the function that sets the main heading of the plot.
❓ Predict Output
intermediate
2:00remaining
What label appears on the y-axis?
Look at this code snippet. What label will appear on the y-axis of the plot?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([10, 20, 30], [1, 4, 9])
plt.xlabel('Time (s)')
plt.ylabel('Distance (m)')
plt.title('Motion Data')
plt.show()
ATime (s)
BDistance (m)
CMotion Data
DSpeed (m/s)
Attempts:
2 left
💡 Hint
The y-axis label is set by the function that starts with 'ylabel'.
🔧 Debug
advanced
2:00remaining
Why does this code not show the x-axis label?
This code is supposed to show a plot with an x-axis label, but the label does not appear. What is the reason?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [3, 2, 1])
plt.xlabel('Time')
plt.title('Sample Plot')
plt.show()
AThe x-axis label is set correctly and will appear as 'Time'.
BThe plot command is missing, so no plot is shown.
CThe plt.xlabel() function is called after plt.show(), so it does not update the plot.
DThe x-axis label is not shown because plt.title() overrides it.
Attempts:
2 left
💡 Hint
Check the order of commands and what plt.show() does.
❓ visualization
advanced
2:00remaining
Identify the axis labels and title from the plot
Given this code, what are the x-axis label, y-axis label, and title of the plot?
Matplotlib
import matplotlib.pyplot as plt
x = [0, 1, 2, 3]
y = [0, 1, 4, 9]
plt.plot(x, y)
plt.xlabel('Input')
plt.ylabel('Output')
plt.title('Quadratic Growth')
plt.show()
Ax-axis: Input, y-axis: Output, title: Quadratic Growth
Bx-axis: Output, y-axis: Input, title: Quadratic Growth
Cx-axis: Input, y-axis: Quadratic Growth, title: Output
Dx-axis: Quadratic Growth, y-axis: Output, title: Input
Attempts:
2 left
💡 Hint
Look at the functions plt.xlabel(), plt.ylabel(), and plt.title().
🧠 Conceptual
expert
2:00remaining
What happens if you call plt.xlabel() twice with different labels before plt.show()?
If you run this code, what will be the label shown on the x-axis?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [3, 2, 1])
plt.xlabel('First Label')
plt.xlabel('Second Label')
plt.title('Test Plot')
plt.show()
ANo x-axis label will appear because of the conflict.
BThe x-axis label will be 'First Label'.
CBoth labels will appear on the x-axis.
DThe x-axis label will be 'Second Label'.
Attempts:
2 left
💡 Hint
Think about what happens when you set the same property twice.

Practice

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

Solution

  1. Step 1: Understand the purpose of each function

    plt.title() is designed to add a title to the plot, while the others do not exist or serve this purpose.
  2. Step 2: Confirm the correct function for title

    The function plt.title() correctly adds a title to the plot.
  3. Final Answer:

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

    Title function = plt.title() [OK]
Hint: Title your plot with plt.title() for clarity [OK]
Common Mistakes:
  • Using plt.label() instead of plt.title()
  • Confusing axis labels with title
  • Trying plt.name() which doesn't exist
2. Which of the following is the correct syntax to label the x-axis as 'Time (s)' in matplotlib?
easy
A. plt.axislabel('Time (s)')
B. plt.xaxis('Time (s)')
C. plt.labelx('Time (s)')
D. plt.xlabel('Time (s)')

Solution

  1. Step 1: Recall the correct function for x-axis label

    The function to label the x-axis is plt.xlabel().
  2. Step 2: Check syntax correctness

    plt.xlabel('Time (s)') uses the correct function and syntax: plt.xlabel('Time (s)').
  3. Final Answer:

    plt.xlabel('Time (s)') -> Option D
  4. Quick Check:

    X-axis label = plt.xlabel() [OK]
Hint: Use plt.xlabel() to name the x-axis quickly [OK]
Common Mistakes:
  • Using plt.xaxis() which does not label axis
  • Trying plt.labelx() which is invalid
  • Confusing axis label with axis limits
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', and y-axis labeled 'Sales'
D. A plot with axis labels but no title

Solution

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

    The code uses plt.title('Sales Over Time'), plt.xlabel('Month'), and plt.ylabel('Sales') to add title and axis labels.
  2. Step 2: Understand the plot output

    The plot will display the line graph with the specified title and axis labels visible.
  3. Final Answer:

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

    Title and labels set = A plot with title 'Sales Over Time', x-axis labeled 'Month', and y-axis labeled 'Sales' [OK]
Hint: Check plt.title(), plt.xlabel(), plt.ylabel() for labels [OK]
Common Mistakes:
  • Assuming labels won't show without plt.legend()
  • Forgetting plt.show() to display plot
  • Confusing axis labels with tick labels
4. Identify the error in this code snippet:
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. Incorrect assignment to plt.title instead of calling plt.title()
B. plt.xlabel() is missing parentheses
C. plt.plot() has wrong data format
D. plt.show() is missing

Solution

  1. Step 1: Check how title is set

    The code uses plt.title = 'My Plot', which assigns a string to the function name, breaking it.
  2. Step 2: Correct way to set title

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

    Incorrect assignment to plt.title instead of calling plt.title() -> Option A
  4. Quick Check:

    Title must be set by plt.title() function call [OK]
Hint: Use plt.title('text'), not plt.title = 'text' [OK]
Common Mistakes:
  • Assigning string to plt.title instead of calling it
  • Forgetting parentheses on function calls
  • Confusing plt.title with plt.xlabel
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 does this and also sets the font size of the title to 16?
hard
A. plt.title('Temperature Trends', fontsize=16)\nplt.xlabel('Day')\nplt.ylabel('Temperature (°C)')
B. plt.title('Temperature Trends')\nplt.xlabel('Day', fontsize=16)\nplt.ylabel('Temperature (°C)')
C. plt.title('Temperature Trends', size=16)\nplt.xlabel('Day')\nplt.ylabel('Temperature (°C)')
D. plt.title('Temperature Trends')\nplt.xlabel('Day')\nplt.ylabel('Temperature (°C)', fontsize=16)

Solution

  1. Step 1: Identify how to set title font size

    The fontsize parameter in plt.title() sets the title font size correctly.
  2. Step 2: Check axis labels and font size

    Only the title should have font size 16; axis labels do not need it here.
  3. Final Answer:

    plt.title('Temperature Trends', fontsize=16) plt.xlabel('Day') plt.ylabel('Temperature (°C)') -> Option A
  4. Quick Check:

    Title font size set by fontsize=16 in plt.title() [OK]
Hint: Use fontsize=16 inside plt.title() to resize title text [OK]
Common Mistakes:
  • Using size=16 instead of fontsize=16
  • Setting fontsize on axis labels instead of title
  • Forgetting to call plt.title() as a function
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