Scatter Plots: Relationships Between Variables
You'll learn how to create scatter plots that reveal correlations by customizing markers, sizes, colors, and transparency.
What Are Scatter Plots?
A scatter plot displays individual data points as dots on a two-dimensional grid, with one variable on the x-axis and another on the y-axis. Think of it as dropping pins on a map—each pin's position tells a story about how two measurements relate. Clustering patterns reveal correlations: upward slopes suggest positive relationships, downward slopes negative ones, and scattered randomness indicates no clear pattern.
Customizing Scatter Plots
Beyond basic dots, Matplotlib lets you encode additional information through visual customization:
Marker styles change the shape of each point (circles, squares, triangles, etc.) to distinguish categories or datasets.
Size variation can represent a third numeric variable—larger bubbles for higher values create a "bubble chart" effect.
Color mapping adds another dimension, using different hues or intensity to show groupings or continuous scales.
Transparency (alpha) prevents overplotting in dense datasets. When hundreds of points overlap, semi-transparent markers let you see concentration patterns rather than just a solid blob.
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 5, 4, 6]
sizes = [50, 100, 150, 200, 250]
colors = [10, 20, 30, 40, 50]
plt.scatter(x, y, s=sizes, c=colors, alpha=0.6, cmap='viridis', marker='o')
plt.xlabel('Variable X')
plt.ylabel('Variable Y')
plt.title('Customized Scatter Plot')
plt.colorbar(label='Third Variable')
plt.show()
Key Takeaway: Scatter plots visualize relationships between two variables, and customizing marker properties (size, color, transparency, shape) transforms them into rich multi-dimensional exploration tools for uncovering patterns and correlations.