Basic Scatter Plot
Create a simple scatter plot:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.scatter(x, y)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Scatter Plot')
plt.show()
Customizing Points
Customize point appearance:
plt.scatter(x, y,
s=100, # Size
c='red', # Color
alpha=0.5, # Transparency
marker='o', # Marker style
edgecolors='black', # Edge color
linewidths=2) # Edge width
Color Mapping
Color points by value:
import numpy as np
x = np.random.rand(50)
y = np.random.rand(50)
colors = np.random.rand(50)
sizes = 1000 * np.random.rand(50)
plt.scatter(x, y, c=colors, s=sizes, alpha=0.5)
plt.colorbar() # Show color scale
plt.show()
Multiple Groups
Plot multiple groups:
x1, y1 = [1, 2, 3], [2, 4, 6]
x2, y2 = [1, 2, 3], [3, 5, 7]
plt.scatter(x1, y1, label='Group 1', color='blue')
plt.scatter(x2, y2, label='Group 2', color='red')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Multiple Groups')
plt.legend()
plt.show()
Best Practices
- Use appropriate size: Points should be visible but not overlap
- Add colorbar: When using color mapping
- Use transparency: For overlapping points
- Label axes: Always include axis labels
Next Steps
Learn about line plots or bar charts.