Concept Flow - Scatter plots
Start with DataFrame
Select two columns
Call plot.scatter(x, y)
Matplotlib creates scatter plot
Display plot to user
End
Scatter plots show how two variables relate by plotting points for each pair of values.
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]}) df.plot.scatter(x='x', y='y') plt.show()
| Step | Action | DataFrame State | Plot State | Output |
|---|---|---|---|---|
| 1 | Create DataFrame with columns 'x' and 'y' | {'x': [1,2,3], 'y': [4,5,6]} | No plot yet | DataFrame ready |
| 2 | Call df.plot.scatter(x='x', y='y') | DataFrame unchanged | Scatter plot object created | Plot object ready |
| 3 | Matplotlib prepares plot | DataFrame unchanged | Points plotted at (1,4), (2,5), (3,6) | Plot ready to display |
| 4 | Call plt.show() | DataFrame unchanged | Plot displayed on screen | Scatter plot visible |
| 5 | End of code | DataFrame unchanged | Plot displayed | Execution complete |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|---|
| df | None | {'x': [1,2,3], 'y': [4,5,6]} | {'x': [1,2,3], 'y': [4,5,6]} | {'x': [1,2,3], 'y': [4,5,6]} | {'x': [1,2,3], 'y': [4,5,6]} | {'x': [1,2,3], 'y': [4,5,6]} |
| plot | None | None | Scatter plot object | Scatter plot with points | Displayed plot | Displayed plot |
Scatter plots show points for two variables on x and y axes. Use df.plot.scatter(x='col1', y='col2') to create. Matplotlib draws points for each row. Call plt.show() to display the plot. DataFrame stays unchanged during plotting.