Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to plot additional points on the top of a scatter plot in Matplotlib?
Matplotlib allows you to add additional points on top of existing scatter plots. This is useful for highlighting specific data points or overlaying different datasets with distinct markers.
Steps
Create initial scatter plot with base data points
Use
plt.plot()orplt.scatter()to add additional pointsCustomize markers using
marker,markersize, andcolorparametersDisplay the plot using
show()method
Basic Example
Here's how to add star markers on top of a scatter plot ?
import matplotlib.pyplot as plt
# Base data points
x = [1, 2, 6, 4]
y = [1, 5, 2, 3]
# Create scatter plot
plt.figure(figsize=(8, 5))
plt.scatter(x, y, c='blue', s=50, alpha=0.7, label='Base points')
# Add additional points on top
plt.plot([3, 4], [6, 7], marker='*', color='red', markersize=15,
linestyle='none', label='Additional points')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Scatter Plot with Additional Points')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
[A scatter plot showing blue circles as base points and red stars as additional points overlaid on top]
Using Different Markers
You can use various marker styles to distinguish different point types ?
import matplotlib.pyplot as plt
import numpy as np
# Generate sample data
np.random.seed(42)
x1 = np.random.randn(50)
y1 = np.random.randn(50)
# Create base scatter plot
plt.figure(figsize=(8, 6))
plt.scatter(x1, y1, c='lightblue', s=40, alpha=0.6, label='Dataset 1')
# Add different types of additional points
plt.plot([-1, 1], [2, -2], marker='s', color='red', markersize=12,
linestyle='none', label='Important points')
plt.plot([0], [0], marker='^', color='green', markersize=15,
linestyle='none', label='Center point')
plt.plot([2, -2], [1, -1], marker='D', color='orange', markersize=10,
linestyle='none', label='Special markers')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Multiple Marker Types on Scatter Plot')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
[A scatter plot showing light blue circles with various colored markers (squares, triangles, diamonds) overlaid]
Using scatter() for Additional Points
You can also use scatter() instead of plot() for more control over individual point properties ?
import matplotlib.pyplot as plt
# Base data
x_base = [1, 2, 3, 4, 5]
y_base = [2, 4, 1, 3, 5]
# Additional points data
x_additional = [2.5, 4.5]
y_additional = [3.5, 2.5]
plt.figure(figsize=(8, 5))
# Base scatter plot
plt.scatter(x_base, y_base, c='blue', s=80, alpha=0.7, label='Original data')
# Add additional points with different sizes and colors
plt.scatter(x_additional, y_additional, c='red', s=200, marker='*',
alpha=0.9, label='Highlighted points', edgecolors='black', linewidth=1)
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Adding Points with scatter() Method')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
[A scatter plot showing blue circles with large red stars with black borders overlaid]
Key Parameters
| Parameter | Description | Example Values |
|---|---|---|
marker |
Shape of the marker | '*', 's', '^', 'D', 'o' |
markersize / s
|
Size of the marker | 10, 50, 200 |
color / c
|
Color of the marker | 'red', '#FF0000', (1,0,0) |
alpha |
Transparency level | 0.1 to 1.0 |
Conclusion
Use plt.plot() with linestyle='none' or plt.scatter() to overlay additional points on scatter plots. Choose appropriate markers and colors to distinguish different data categories effectively.
