Plot customization (title, labels, figsize) in Pandas - Time & Space Complexity
We want to see how changing plot settings affects the time it takes to create a plot.
How does adding titles, labels, or changing size change the work done?
Analyze the time complexity of the following code snippet.
import pandas as pd
import matplotlib.pyplot as plt
data = pd.Series(range(1000))
ax = data.plot(figsize=(8, 4))
ax.set_title('Sample Plot')
ax.set_xlabel('Index')
ax.set_ylabel('Value')
plt.close()
This code creates a line plot from 1000 points and adds a title and axis labels with a set figure size.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Plotting the 1000 data points (drawing each point).
- How many times: Once for each data point (1000 times).
Adding titles, labels, and figure size settings takes about the same time no matter the data size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | ~10 drawing operations + fixed title/label setup |
| 100 | ~100 drawing operations + fixed title/label setup |
| 1000 | ~1000 drawing operations + fixed title/label setup |
Pattern observation: The main work grows with the number of points, but title and label setup stays constant.
Time Complexity: O(n)
This means the time grows linearly with the number of data points, while adding titles and labels does not add extra growth.
[X] Wrong: "Adding a title or labels makes the plot take much longer as data grows."
[OK] Correct: Titles and labels are set once and do not depend on data size, so they add only a small fixed cost.
Understanding which parts of code grow with data size helps you explain your choices clearly and confidently.
"What if we added a loop to create multiple plots each with titles and labels? How would the time complexity change?"
Practice
figsize parameter control when creating a plot with pandas?Solution
Step 1: Understand the role of
Thefigsizefigsizeparameter sets the width and height of the plot in inches.Step 2: Differentiate from other parameters
Color, font size, and plot type are controlled by other parameters, notfigsize.Final Answer:
The size of the plot figure in inches (width, height). -> Option DQuick Check:
Figsize = plot size in inches [OK]
- Confusing figsize with color or font size
- Thinking figsize changes plot type
- Using figsize as a string instead of tuple
Solution
Step 1: Recall pandas plot method chaining
After plotting, you can chainset_title(),set_xlabel(), andset_ylabel()to set title and labels.Step 2: Check syntax correctness
df.plot().set_title('Sales').set_xlabel('Month').set_ylabel('Revenue') uses correct method names and chaining syntax. df.plot(title='Sales', x_label='Month', y_label='Revenue') tries to pass labels directly toplot()which is invalid. df.plot().set(title='Sales', xlabel='Month', ylabel='Revenue') usesset()with wrong keys. df.plot().title('Sales').xlabel('Month').ylabel('Revenue') uses non-existent methods.Final Answer:
df.plot().set_title('Sales').set_xlabel('Month').set_ylabel('Revenue') -> Option AQuick Check:
Use set_title, set_xlabel, set_ylabel methods [OK]
- Passing title or labels directly to plot()
- Using wrong method names like title() instead of set_title()
- Trying to set labels with set() method
import pandas as pd
import matplotlib.pyplot as plt
data = {'A': [1, 3, 2, 4]}
df = pd.DataFrame(data)
ax = df.plot(figsize=(8, 4))
plt.show()Solution
Step 1: Understand figsize parameter
Thefigsizeargument takes a tuple (width, height) in inches.Step 2: Check the given tuple
The tuple is (8, 4), so width is 8 inches and height is 4 inches.Final Answer:
8 inches wide and 4 inches tall -> Option AQuick Check:
Figsize=(8,4) means width=8, height=4 inches [OK]
- Swapping width and height values
- Thinking figsize uses pixels
- Assuming figsize is ignored by pandas
import pandas as pd
data = {'x': [1, 2, 3], 'y': [4, 5, 6]}
df = pd.DataFrame(data)
plot = df.plot()
plot.title('My Plot')
plot.xlabel('X-axis')
plot.ylabel('Y-axis')Solution
Step 1: Check method names on plot object
The plot object returned by pandas usesset_title(),set_xlabel(), andset_ylabel()methods, nottitle(),xlabel(), orylabel().Step 2: Identify the error
Callingplot.title()causes an AttributeError because the method does not exist.Final Answer:
Methods title(), xlabel(), ylabel() do not exist for the plot object. -> Option BQuick Check:
Use set_title, set_xlabel, set_ylabel methods [OK]
- Using title() instead of set_title()
- Forgetting to call plt.show() (though not error here)
- Assuming DataFrame columns affect label setting
df with columns 'Year' and 'Sales'. You want the plot to be 10 inches wide and 6 inches tall, with the title 'Annual Sales', x-axis label 'Year', and y-axis label 'Sales'. Which code snippet correctly achieves this?Solution
Step 1: Set figure size correctly
Figure size must be set in theplot.bar()call usingfigsize=(10,6). ax = df.plot.bar(x='Year', y='Sales', figsize=(10,6)) ax.set_title('Annual Sales') ax.set_xlabel('Year') ax.set_ylabel('Sales') does this correctly.Step 2: Set title and axis labels on the Axes object
ax = df.plot.bar(x='Year', y='Sales', figsize=(10,6)) ax.set_title('Annual Sales') ax.set_xlabel('Year') ax.set_ylabel('Sales') usesax.set_title(),ax.set_xlabel(), andax.set_ylabel()which is the correct way to set labels after plotting.Step 3: Check other options
df.plot.bar(x='Year', y='Sales', title='Annual Sales', x_label='Year', y_label='Sales', figsize=(10,6)) tries to pass title and labels directly toplot.bar(), which is not supported. ax = df.plot.bar(x='Year', y='Sales') plt.title('Annual Sales') plt.xlabel('Year') plt.ylabel('Sales') plt.figure(figsize=(10,6)) sets figure size after plotting, which has no effect. df.plot.bar(x='Year', y='Sales') ax.set_title('Annual Sales') ax.set_xlabel('Year') ax.set_ylabel('Sales') plt.figure(figsize=(10,6)) usesaxwithout defining it and sets figure size after plotting, which is incorrect.Final Answer:
ax = df.plot.bar(x='Year', y='Sales', figsize=(10,6)) ax.set_title('Annual Sales') ax.set_xlabel('Year') ax.set_ylabel('Sales') -> Option CQuick Check:
Set figsize in plot call; use set_title and set_xlabel on Axes [OK]
- Passing title and labels directly to plot.bar()
- Setting figure size after plotting
- Using undefined Axes object
Ready to prove it?
Pass the test and this pattern moves unknown
