Scatter plots in Pandas - Time & Space Complexity
We want to understand how the time to create a scatter plot changes as the data size grows.
How does plotting many points affect the time it takes?
Analyze the time complexity of the following code snippet.
import pandas as pd
import matplotlib.pyplot as plt
n = 1000 # example size
data = pd.DataFrame({
'x': range(n),
'y': range(n)
})
plt.scatter(data['x'], data['y'])
plt.show()
This code creates a scatter plot of n points using pandas and matplotlib.
- Primary operation: Plotting each point on the scatter plot.
- How many times: Once for each of the n points in the data.
As the number of points increases, the time to plot grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations (plotting 10 points) |
| 100 | 100 operations (plotting 100 points) |
| 1000 | 1000 operations (plotting 1000 points) |
Pattern observation: The time grows linearly as the number of points increases.
Time Complexity: O(n)
This means the time to create the scatter plot grows directly with the number of points.
[X] Wrong: "Plotting a scatter plot takes the same time no matter how many points there are."
[OK] Correct: Each point must be drawn, so more points mean more work and more time.
Understanding how plotting time grows helps you explain performance when working with large datasets in real projects.
"What if we used a sampling method to plot only a fraction of points? How would the time complexity change?"
Practice
Solution
Step 1: Understand scatter plot purpose
A scatter plot is used to visualize how two numeric variables relate to each other by plotting points on an x and y axis.Step 2: Match with options
Only The relationship between two numeric columns describes this relationship between two numeric columns, while others describe different chart types or summaries.Final Answer:
The relationship between two numeric columns -> Option AQuick Check:
Scatter plot = relationship between two numeric columns [OK]
- Confusing scatter plots with bar charts
- Thinking scatter plots show single column frequency
- Assuming scatter plots summarize data instead of showing relationships
Solution
Step 1: Recall pandas scatter plot syntax
The correct method is df.plot.scatter with x and y as column names in quotes.Step 2: Check each option
df.plot.scatter(x='height', y='weight') uses correct method and argument format. df.scatter_plot('height', 'weight') is invalid method. df.plot.scatter('weight', 'height') swaps x and y but is still valid syntax, but question asks for 'height' as x and 'weight' as y. df.plot.scatter(x=height, y=weight) uses variables without quotes causing error.Final Answer:
df.plot.scatter(x='height', y='weight') -> Option CQuick Check:
Correct syntax uses df.plot.scatter with x and y as strings [OK]
- Using method scatter_plot instead of plot.scatter
- Passing column names without quotes
- Swapping x and y columns unintentionally
import pandas as pd
import matplotlib.pyplot as plt
data = {'age': [25, 30, 35, 40], 'income': [50000, 60000, 65000, 70000]}
df = pd.DataFrame(data)
df.plot.scatter(x='age', y='income')
plt.show()What will the scatter plot show?
Solution
Step 1: Understand the code
The DataFrame has 'age' and 'income' columns. The scatter plot plots 'age' on x-axis and 'income' on y-axis.Step 2: Interpret the plot type and data
Scatter plot will show points representing each person's age and income. Income increases as age increases, so points trend upward.Final Answer:
Points showing income increasing with age -> Option DQuick Check:
Scatter plot shows points, not bars or lines [OK]
- Confusing scatter plot with bar or line plot
- Thinking plt.show() is optional and causes error
- Misreading which column is x or y
df.plot.scatter(x=age, y='income')
Solution
Step 1: Check the x parameter
The x parameter should be the column name as a string, but age is unquoted, treated as a variable.Step 2: Verify other parameters and method
y='income' is correct as a string. plot.scatter is valid pandas method. So only x parameter is wrong.Final Answer:
The variable age is not defined or quoted -> Option BQuick Check:
Column names must be strings in plot.scatter [OK]
- Passing column names without quotes
- Confusing pandas plot.scatter with matplotlib scatter
- Thinking y must be numeric type, not string
Solution
Step 1: Understand color parameter in scatter plot
The c parameter expects numeric values for colors. The 'day' column is categorical (strings), so it must be converted to numeric codes.Step 2: Check each option's color argument
Use df.plot.scatter(x='temperature', y='sales', c=df['day'].astype('category').cat.codes) converts 'day' to category codes, which is correct. Use df.plot.scatter(x='temperature', y='sales', color='day') uses color='day' which is invalid. Use df.plot.scatter(x='temperature', y='sales', c='day') passes string 'day' which is invalid. Use df.plot.scatter(x='temperature', y='sales', c=df['day']) passes categorical strings directly, causing error.Final Answer:
Use df.plot.scatter(x='temperature', y='sales', c=df['day'].astype('category').cat.codes) -> Option AQuick Check:
Color values must be numeric codes for categorical data [OK]
- Passing categorical strings directly to color parameter
- Using color='day' instead of numeric codes
- Not converting categories before coloring
Ready to prove it?
Pass the test and this pattern moves unknown
