Changing marker size helps show differences in data points clearly on a plot.
Marker size variation in Matplotlib
Introduction
Syntax
Matplotlib
plt.scatter(x, y, s=size_values) # or plt.plot(x, y, marker='o', markersize=size_value)
s in plt.scatter controls marker size and can be a single number or a list of sizes.
markersize in plt.plot sets the size of markers when using line plots.
Examples
Matplotlib
import matplotlib.pyplot as plt x = [1, 2, 3] y = [4, 5, 6] plt.scatter(x, y, s=100) plt.show()
Matplotlib
import matplotlib.pyplot as plt x = [1, 2, 3] y = [4, 5, 6] sizes = [20, 50, 200] plt.scatter(x, y, s=sizes) plt.show()
plt.plot with marker size 15.Matplotlib
import matplotlib.pyplot as plt x = [1, 2, 3] y = [4, 5, 6] plt.plot(x, y, marker='o', markersize=15) plt.show()
Sample Program
This code creates a scatter plot where each point's size shows its importance or value. Bigger points stand out more.
Matplotlib
import matplotlib.pyplot as plt # Data points x = [1, 2, 3, 4, 5] y = [5, 7, 4, 6, 8] # Sizes represent importance or value sizes = [50, 200, 100, 300, 150] plt.scatter(x, y, s=sizes, color='blue', alpha=0.6) plt.title('Scatter plot with marker size variation') plt.xlabel('X axis') plt.ylabel('Y axis') plt.show()
Important Notes
Marker size s in plt.scatter is in points squared, so 100 means 10x10 points.
Use alpha to make markers transparent if they overlap.
Marker size can help show extra data dimension visually.
Summary
Marker size changes how big points look on a plot.
You can use a single size or a list of sizes for different points.
It helps add meaning and clarity to your charts.
Practice
1. What does the
s parameter control in the plt.scatter() function in matplotlib?easy
Solution
Step 1: Understand the role of
Theplt.scatter()plt.scatter()function creates scatter plots where each point can have different visual properties.Step 2: Identify the
Thesparameter purposesparameter specifically sets the size of each marker in the scatter plot, either as a single value or a list of sizes.Final Answer:
The size of the markers in the scatter plot -> Option AQuick Check:
scontrols marker size [OK]
Hint: Remember: 's' stands for size in scatter plots [OK]
Common Mistakes:
- Confusing 's' with color or shape parameters
- Thinking 's' controls transparency
- Assuming 's' changes marker style
2. Which of the following is the correct way to set marker sizes to 50 for all points in a scatter plot using matplotlib?
easy
Solution
Step 1: Recall the parameter name for marker size
The correct parameter to set marker size inplt.scatter()iss.Step 2: Check the syntax for setting size
Usings=50sets all markers to size 50. Other options use incorrect parameter names.Final Answer:
plt.scatter(x, y, s=50) -> Option BQuick Check:
Usesfor size [OK]
Hint: Use
s to set size, not 'size' or 'sizes' [OK]Common Mistakes:
- Using 'size' or 'sizes' instead of 's'
- Trying to set marker size with 'marker_size'
- Syntax errors from wrong parameter names
3. What will be the output marker sizes for the scatter plot from this code?
import matplotlib.pyplot as plt x = [1, 2, 3] y = [4, 5, 6] sizes = [20, 50, 80] plt.scatter(x, y, s=sizes) plt.show()
medium
Solution
Step 1: Understand
Theswith list inputsparameter accepts a list to assign different sizes to each marker.Step 2: Match sizes to points
Sincesizes = [20, 50, 80], the first point gets size 20, second 50, third 80.Final Answer:
Markers will have sizes 20, 50, and 80 respectively -> Option DQuick Check:
List inssets sizes per point [OK]
Hint: List in
s sets sizes point-wise [OK]Common Mistakes:
- Assuming all markers get the first size
- Thinking list input causes error
- Confusing
swith color list
4. Identify the error in this code snippet that tries to vary marker sizes:
import matplotlib.pyplot as plt x = [1, 2, 3] y = [4, 5, 6] sizes = [30, 60] plt.scatter(x, y, s=sizes) plt.show()
medium
Solution
Step 1: Check lengths of data and sizes
The x and y lists have 3 points, but sizes list has only 2 values.Step 2: Understand size list requirement
Theslist must match the number of points; otherwise, matplotlib raises a ValueError.Final Answer:
The sizes list length does not match x and y length -> Option CQuick Check:
Sizes list length must equal data points [OK]
Hint: Match sizes list length to data points [OK]
Common Mistakes:
- Ignoring size list length mismatch
- Thinking
scan't be a list - Assuming matplotlib auto-fills missing sizes
5. You want to create a scatter plot where marker sizes represent the population of cities. Given:
Which code correctly scales the population to marker sizes and plots them?
cities = ['A', 'B', 'C'] population = [1000, 5000, 3000] x = [1, 2, 3] y = [4, 5, 6]
Which code correctly scales the population to marker sizes and plots them?
hard
Solution
Step 1: Understand marker size scaling
Population values are large; directly using them as sizes will create huge markers.Step 2: Choose appropriate scaling
Multiplying population by 10 scales sizes visibly but keeps them reasonable. Dividing by 10 makes markers too small; squaring makes them too large.Final Answer:
plt.scatter(x, y, s=[p*10 for p in population]) -> Option AQuick Check:
Scale sizes for visibility using multiplication [OK]
Hint: Scale data to reasonable marker sizes by multiplying [OK]
Common Mistakes:
- Using raw large values causing huge markers
- Dividing sizes too much making markers tiny
- Using squared sizes causing disproportionate markers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
