Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Labels, titles, and legends in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Legend and Title Master
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?
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()
ASales Over Time
BSales Data
CMonth
DRevenue
Attempts:
2 left
💡 Hint
Look for the plt.title() function argument.
❓ data_output
intermediate
1: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()
A2
B3
C0
D1
Attempts:
2 left
💡 Hint
Count how many lines have a label argument.
🔧 Debug
advanced
2: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()
AThe plt.show() function must be called before plotting lines.
BThe labels are incorrect and cause an error preventing the legend.
CThe plt.legend() function is missing, so no legend is displayed.
DThe plot lines do not have colors, so the legend is hidden.
Attempts:
2 left
💡 Hint
Check if the legend function is called explicitly.
❓ visualization
advanced
2: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()
Aplt.legend(legend_title='Function Types')
Bplt.legend(label='Function Types')
Cplt.legend(title_label='Function Types')
Dplt.legend(title='Function Types')
Attempts:
2 left
💡 Hint
Check the official parameter name for legend title.
🧠 Conceptual
expert
2: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()
AThe plot will show both titles concatenated.
BThe plot will show 'Second Title' as the title.
CThe plot will show 'First Title' as the title.
DThe plot will have no title because of conflict.
Attempts:
2 left
💡 Hint
Think about how the title function updates the title property.

Practice

(1/5)
1. What is the main purpose of adding a title to a chart in data visualization?
easy
A. To give the chart a clear heading that explains what it shows
B. To label the x-axis with numbers
C. To add colors to the chart
D. To describe the data source

Solution

  1. Step 1: Understand the role of a title in charts

    A title provides a clear heading that tells the viewer what the chart is about.
  2. Step 2: Compare options with the title's purpose

    Only To give the chart a clear heading that explains what it shows correctly describes the purpose of a title as a clear heading.
  3. Final Answer:

    To give the chart a clear heading that explains what it shows -> Option A
  4. Quick Check:

    Title = Clear heading [OK]
Hint: Titles are headings that explain the whole chart [OK]
Common Mistakes:
  • Confusing titles with axis labels
  • Thinking titles add colors
  • Mixing titles with legends
2. Which of the following is the correct way to add a legend to a Matplotlib plot in Python?
easy
A. plt.xlabel('Legend')
B. plt.title('Legend')
C. plt.legend()
D. plt.legend('show')

Solution

  1. Step 1: Recall the Matplotlib function for legends

    The function plt.legend() adds a legend to the plot.
  2. Step 2: Check other options for correctness

    Options A and B set x-axis label and title, not legend. plt.legend('show') uses incorrect syntax.
  3. Final Answer:

    plt.legend() -> Option C
  4. Quick Check:

    Legend function = plt.legend() [OK]
Hint: Use plt.legend() to show legends in Matplotlib [OK]
Common Mistakes:
  • Using plt.title() to add legend
  • Passing strings inside plt.legend() incorrectly
  • Confusing xlabel with legend
3. What will be the output of this code snippet?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='Line 1')
plt.plot([3, 2, 1], label='Line 2')
plt.title('Sample Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.legend()
plt.show()
medium
A. A plot with two lines, a title, axis labels, and a legend showing 'Line 1' and 'Line 2'
B. A plot with no title and no legend
C. A plot with only one line and no labels
D. An error because legend labels are missing

Solution

  1. Step 1: Analyze the plot commands

    Two lines are plotted with labels 'Line 1' and 'Line 2'. Title and axis labels are set.
  2. Step 2: Understand legend and show functions

    Calling plt.legend() displays the legend with the given labels. plt.show() displays the plot.
  3. Final Answer:

    A plot with two lines, a title, axis labels, and a legend showing 'Line 1' and 'Line 2' -> Option A
  4. Quick Check:

    Legend and labels appear correctly [OK]
Hint: Labels in plot() + plt.legend() show legends automatically [OK]
Common Mistakes:
  • Forgetting to call plt.legend()
  • Assuming legend appears without labels
  • Confusing axis labels with legend
4. Identify the error in this code that prevents the legend from showing:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.plot([3, 2, 1])
plt.legend()
plt.show()
medium
A. plt.legend() is called before plt.plot()
B. Missing labels in plot() calls for legend
C. plt.show() is missing
D. plt.title() is required for legend

Solution

  1. Step 1: Check plot() calls for labels

    Both plot calls lack the label argument, so legend has no text to display.
  2. Step 2: Verify legend and show usage

    Calling plt.legend() without labels results in an empty legend. plt.show() is present and plt.title() is not required.
  3. Final Answer:

    Missing labels in plot() calls for legend -> Option B
  4. Quick Check:

    Labels needed for legend [OK]
Hint: Add label='name' in plot() to show legend [OK]
Common Mistakes:
  • Calling plt.legend() before plot()
  • Thinking plt.title() affects legend
  • Forgetting to call plt.show()
5. You have a DataFrame with sales data for two products over 3 months. You want to plot both lines on the same chart with a title, axis labels, and a legend. Which code snippet correctly adds all these elements?
import pandas as pd
import matplotlib.pyplot as plt

data = {'Month': ['Jan', 'Feb', 'Mar'], 'ProductA': [100, 120, 130], 'ProductB': [90, 110, 115]}
df = pd.DataFrame(data)

# Which code below is correct?

A: plt.plot(df['Month'], df['ProductA'], label='Product A')
   plt.plot(df['Month'], df['ProductB'], label='Product B')
   plt.title('Sales Over Months')
   plt.xlabel('Month')
   plt.ylabel('Sales')
   plt.legend()
   plt.show()

B: plt.plot(df['ProductA'], df['Month'], label='Product A')
   plt.plot(df['ProductB'], df['Month'], label='Product B')
   plt.title('Sales Over Months')
   plt.xlabel('Sales')
   plt.ylabel('Month')
   plt.legend()
   plt.show()

C: plt.plot(df['Month'], df['ProductA'])
   plt.plot(df['Month'], df['ProductB'])
   plt.title('Sales Over Months')
   plt.xlabel('Month')
   plt.ylabel('Sales')
   plt.show()

D: plt.plot(df['Month'], df['ProductA'], label='Product A')
   plt.plot(df['Month'], df['ProductB'], label='Product B')
   plt.xlabel('Month')
   plt.ylabel('Sales')
   plt.show()
hard
A. Missing title and legend, so incomplete chart
B. Swaps x and y axes and labels, causing incorrect plot
C. Missing legend call, so no legend shown
D. Correctly plots both products with labels, title, axis labels, and legend

Solution

  1. Step 1: Check axis data and labels in each option

    Correctly plots both products with labels, title, axis labels, and legend uses Month on x-axis and sales on y-axis correctly with labels and title.
  2. Step 2: Verify legend and title presence

    Correctly plots both products with labels, title, axis labels, and legend includes label in plot calls and calls plt.legend() and plt.title(). Others miss legend or swap axes.
  3. Final Answer:

    Correctly plots both products with labels, title, axis labels, and legend -> Option D
  4. Quick Check:

    Labels + legend + title = Correctly plots both products with labels, title, axis labels, and legend [OK]
Hint: Match x, y data and add labels + legend + title [OK]
Common Mistakes:
  • Swapping x and y axes
  • Forgetting plt.legend() call
  • Missing title or axis labels
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