Challenge - 5 Problems
Legend and Title Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
What is the title of the plot?
Given the following Python code using matplotlib, what will be the title shown on the plot?
Data Analysis Python
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()
Attempts:
2 left
💡 Hint
Look for the plt.title() function argument.
✗ Incorrect
The plt.title() function sets the title of the plot. Here, it is set to 'Sales Over Time'.
❓ data_output
intermediate1:30remaining
How many legend entries will appear?
Consider this code snippet that plots two lines with labels for the legend. How many entries will the legend show?
Data Analysis Python
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [1, 4, 9], label='Squares') plt.plot([1, 2, 3], [1, 8, 27], label='Cubes') plt.legend() plt.show()
Attempts:
2 left
💡 Hint
Count how many lines have a label argument.
✗ Incorrect
Two lines are plotted, each with a label. The legend shows one entry per labeled line, so it shows 2 entries.
🔧 Debug
advanced2:00remaining
Why does the legend not show?
This code plots two lines with labels but the legend does not appear. What is the reason?
Data Analysis Python
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [2, 4, 6], label='Double') plt.plot([1, 2, 3], [3, 6, 9], label='Triple') plt.show()
Attempts:
2 left
💡 Hint
Check if the legend function is called explicitly.
✗ Incorrect
Matplotlib requires plt.legend() to be called to display the legend. Without it, labels exist but no legend is shown.
❓ visualization
advanced2:00remaining
Which option correctly adds a legend with a title?
You want to add a legend to your plot with the title 'Function Types'. Which code snippet does this correctly?
Data Analysis Python
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [1, 4, 9], label='Squares') plt.plot([1, 2, 3], [1, 8, 27], label='Cubes') # Add legend with title here plt.show()
Attempts:
2 left
💡 Hint
Check the official parameter name for legend title.
✗ Incorrect
The correct parameter to add a title to the legend is 'title'. Other options are invalid and cause errors.
🧠 Conceptual
expert2:00remaining
What happens if you call plt.title() multiple times?
If you call plt.title('First Title') and then plt.title('Second Title') before plt.show(), what will be the plot's title?
Data Analysis Python
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [3, 2, 1]) plt.title('First Title') plt.title('Second Title') plt.show()
Attempts:
2 left
💡 Hint
Think about how the title function updates the title property.
✗ Incorrect
Each call to plt.title() replaces the previous title. The last call sets the final title.