Labels, titles, and legends in Data Analysis Python - Time & Space Complexity
We want to understand how adding labels, titles, and legends affects the time it takes to create a plot.
How does the work grow when we add these elements to a chart?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
x = range(1000)
y = [i * 2 for i in x]
plt.plot(x, y, label='Line 1')
plt.title('Sample Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.legend()
plt.show()
This code creates a line plot with 1000 points and adds a title, axis labels, and a legend.
- Primary operation: Plotting the 1000 points (loop over data points).
- How many times: Once for each of the 1000 points.
- Adding title, labels, and legend are single steps, not repeated.
Adding more data points makes plotting take longer, but adding labels and legends stays the same time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 operations for points + fixed few for labels |
| 100 | About 100 operations for points + fixed few for labels |
| 1000 | About 1000 operations for points + fixed few for labels |
Pattern observation: The work to plot points grows with n, but labels and legends add a small fixed cost.
Time Complexity: O(n)
This means the time grows linearly with the number of data points; labels and legends add only a small fixed time.
[X] Wrong: "Adding a legend or title makes the plot take much longer as data grows."
[OK] Correct: Labels and legends are added once and do not depend on data size, so they add only a small fixed time.
Understanding how different parts of plotting affect time helps you explain performance clearly and shows you can think about code efficiency in real tasks.
"What if we added a legend entry for every data point? How would the time complexity change?"
Practice
title to a chart in data visualization?Solution
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.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.Final Answer:
To give the chart a clear heading that explains what it shows -> Option AQuick Check:
Title = Clear heading [OK]
- Confusing titles with axis labels
- Thinking titles add colors
- Mixing titles with legends
Solution
Step 1: Recall the Matplotlib function for legends
The functionplt.legend()adds a legend to the plot.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.Final Answer:
plt.legend() -> Option CQuick Check:
Legend function = plt.legend() [OK]
- Using plt.title() to add legend
- Passing strings inside plt.legend() incorrectly
- Confusing xlabel with legend
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()Solution
Step 1: Analyze the plot commands
Two lines are plotted with labels 'Line 1' and 'Line 2'. Title and axis labels are set.Step 2: Understand legend and show functions
Callingplt.legend()displays the legend with the given labels.plt.show()displays the plot.Final Answer:
A plot with two lines, a title, axis labels, and a legend showing 'Line 1' and 'Line 2' -> Option AQuick Check:
Legend and labels appear correctly [OK]
- Forgetting to call plt.legend()
- Assuming legend appears without labels
- Confusing axis labels with legend
import matplotlib.pyplot as plt plt.plot([1, 2, 3]) plt.plot([3, 2, 1]) plt.legend() plt.show()
Solution
Step 1: Check plot() calls for labels
Both plot calls lack thelabelargument, so legend has no text to display.Step 2: Verify legend and show usage
Callingplt.legend()without labels results in an empty legend. plt.show() is present and plt.title() is not required.Final Answer:
Missing labels in plot() calls for legend -> Option BQuick Check:
Labels needed for legend [OK]
- Calling plt.legend() before plot()
- Thinking plt.title() affects legend
- Forgetting to call plt.show()
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()Solution
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.Step 2: Verify legend and title presence
Correctly plots both products with labels, title, axis labels, and legend includeslabelin plot calls and callsplt.legend()andplt.title(). Others miss legend or swap axes.Final Answer:
Correctly plots both products with labels, title, axis labels, and legend -> Option DQuick Check:
Labels + legend + title = Correctly plots both products with labels, title, axis labels, and legend [OK]
- Swapping x and y axes
- Forgetting plt.legend() call
- Missing title or axis labels
Ready to prove it?
Pass the test and this pattern moves unknown
