Adding a title and labels to the axes helps people understand what the chart shows. It makes the graph clear and easy to read.
Title and axis labels in Matplotlib
Introduction
Syntax
Matplotlib
plt.title('Your Title') plt.xlabel('X-axis label') plt.ylabel('Y-axis label')
Use plt.title() to add a main title to the plot.
Use plt.xlabel() and plt.ylabel() to name the horizontal and vertical axes.
Examples
Matplotlib
plt.title('Sales Over Time') plt.xlabel('Month') plt.ylabel('Sales ($)')
Matplotlib
plt.title('Temperature vs Time') plt.xlabel('Time (hours)') plt.ylabel('Temperature (°C)')
Sample Program
This program creates a simple line chart with x and y values. It adds a title and labels to the axes so the chart is easy to understand.
Matplotlib
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [10, 20, 25, 30, 40] plt.plot(x, y) plt.title('Example Line Chart') plt.xlabel('X values') plt.ylabel('Y values') plt.show()
Important Notes
You can customize the font size and color by adding parameters like fontsize=14 or color='red' inside the label functions.
Always add axis labels and titles to make your charts clear and professional.
Summary
Use plt.title() to add a chart title.
Use plt.xlabel() and plt.ylabel() to label the axes.
Clear titles and labels help others understand your charts easily.
Practice
1. Which
matplotlib function is used to add a title to a plot?easy
Solution
Step 1: Understand the purpose of
This function adds a title text at the top of the plot, describing what the plot shows.plt.title()Step 2: Compare with other functions
plt.label()does not exist,plt.legend()adds a legend, andplt.axis()controls axis limits, so they don't add a title.Final Answer:
plt.title()-> Option DQuick 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
Solution
Step 1: Recall the function to label x-axis
The correct function isplt.xlabel()which takes the label text as argument.Step 2: Check other options for syntax errors
plt.xaxis(),plt.label(), andplt.axislabel()are not valid matplotlib functions for axis labeling.Final Answer:
plt.xlabel('Time (seconds)') -> Option AQuick 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
Solution
Step 1: Analyze the code lines for title and labels
The code usesplt.title('Sales Over Time')to add a title,plt.xlabel('Month')for x-axis label, andplt.ylabel('Sales')for y-axis label.Step 2: Understand the plot display
The plot will show the line graph with all these labels visible as specified.Final Answer:
A plot with title 'Sales Over Time', x-axis labeled 'Month', y-axis labeled 'Sales' -> Option CQuick 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
Solution
Step 1: Check how title is set
The code usesplt.title = 'My Plot'which assigns a string to the function name, overwriting it instead of calling it.Step 2: Understand correct usage
The correct way is to call the function:plt.title('My Plot')to set the title.Final Answer:
Using assignment operator '=' instead of function call for plt.title -> Option AQuick 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
Solution
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() usesplt.title(),plt.xlabel(), andplt.ylabel()correctly with parentheses and proper arguments.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-existentset_titlefunctions, and plt.plot(days, temps) plt.title('Temperature Trends') plt.xaxis('Day') plt.yaxis('Temperature (°C)') plt.show() uses invalidxaxisandyaxisfunctions.Final Answer:
plt.plot(days, temps) plt.title('Temperature Trends') plt.xlabel('Day') plt.ylabel('Temperature (°C)') plt.show() -> Option BQuick 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
10 questions ~5 mineasy hard
