Skip to content

Scatter Plot Keywords

The ax.scatter() method accepts numerous keyword arguments to control marker appearance and behavior.

Marker Size

The s parameter controls marker size in points squared.

1. Uniform Size

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(42)
x = np.random.rand(50)
y = np.random.rand(50)

fig, ax = plt.subplots()
ax.scatter(x, y, s=100)  # All markers size 100
plt.show()

2. Variable Sizes

sizes = np.random.rand(50) * 500

fig, ax = plt.subplots()
ax.scatter(x, y, s=sizes)
plt.show()

3. Size by Data Value

z = np.random.rand(50) * 100
sizes = z * 5  # Scale data to appropriate marker size

fig, ax = plt.subplots()
ax.scatter(x, y, s=sizes)
plt.show()

Marker Style

The marker parameter sets the marker shape.

1. Common Markers

fig, axes = plt.subplots(2, 4, figsize=(12, 6))
markers = ['o', 's', '^', 'v', 'D', 'p', '*', 'h']
names = ['Circle', 'Square', 'Triangle Up', 'Triangle Down', 
         'Diamond', 'Pentagon', 'Star', 'Hexagon']

for ax, marker, name in zip(axes.flat, markers, names):
    ax.scatter([0.5], [0.5], s=500, marker=marker)
    ax.set_title(name)
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)

plt.tight_layout()
plt.show()

2. Additional Markers

# 'x': X marker
# '+': Plus marker
# '.': Point marker
# ',': Pixel marker
# '1', '2', '3', '4': Tri markers
# '<', '>': Left/Right triangles

3. Custom Marker

from matplotlib.markers import MarkerStyle

# Filled vs unfilled
ax.scatter(x, y, marker='o', facecolors='none', edgecolors='blue')

Color

The c or color parameter controls marker color.

1. Single Color

fig, ax = plt.subplots()
ax.scatter(x, y, color='red')
plt.show()

2. Named Colors

ax.scatter(x, y, color='steelblue')
ax.scatter(x, y, color='coral')
ax.scatter(x, y, color='forestgreen')

3. Hex Colors

ax.scatter(x, y, color='#FF5733')
ax.scatter(x, y, color='#3498DB')

Alpha (Transparency)

The alpha parameter sets marker transparency.

1. Uniform Alpha

fig, ax = plt.subplots()
ax.scatter(x, y, alpha=0.5)
plt.show()

2. Overlapping Points

np.random.seed(42)
x = np.random.normal(0, 1, 1000)
y = np.random.normal(0, 1, 1000)

fig, ax = plt.subplots()
ax.scatter(x, y, alpha=0.3)
plt.show()

3. Reveal Density

fig, axes = plt.subplots(1, 3, figsize=(12, 4))

for ax, alpha in zip(axes, [1.0, 0.5, 0.1]):
    ax.scatter(x, y, alpha=alpha)
    ax.set_title(f'alpha = {alpha}')

plt.tight_layout()
plt.show()

Edge Properties

Control marker edge appearance.

1. Edge Color

fig, ax = plt.subplots()
ax.scatter(x, y, c='lightblue', edgecolors='navy', s=100)
plt.show()

2. Edge Width

ax.scatter(x, y, c='white', edgecolors='black', linewidths=2, s=100)

3. No Edge

ax.scatter(x, y, edgecolors='none', s=100)

Label and Legend

Add labels for legend display.

1. Label Parameter

fig, ax = plt.subplots()
ax.scatter(x[:25], y[:25], label='Group A')
ax.scatter(x[25:], y[25:], label='Group B')
ax.legend()
plt.show()

2. Legend Location

ax.legend(loc='upper right')
ax.legend(loc='lower left')
ax.legend(loc='best')

3. Legend with Marker Size

# Create legend with consistent marker size
scatter = ax.scatter(x, y, s=sizes, label='Data')
ax.legend(markerscale=0.5)  # Scale markers in legend

Z-Order

Control drawing order of overlapping elements.

1. Default Order

fig, ax = plt.subplots()
ax.scatter(x, y, s=200, zorder=1)
ax.axhline(0.5, color='red', zorder=2)  # Line on top
plt.show()

2. Points on Top

fig, ax = plt.subplots()
ax.axhline(0.5, color='red', zorder=1)
ax.scatter(x, y, s=200, zorder=2)  # Points on top
plt.show()

3. Layered Scatter

fig, ax = plt.subplots()
ax.scatter(x, y, s=300, c='lightgray', zorder=1)
ax.scatter(x, y, s=100, c='red', zorder=2)
plt.show()

Combining Keywords

Create complex visualizations with multiple parameters.

1. Styled Points

fig, ax = plt.subplots()
ax.scatter(x, y, 
           s=150,
           c='steelblue',
           alpha=0.7,
           edgecolors='navy',
           linewidths=1,
           marker='o')
plt.show()

2. Bubble Chart Style

np.random.seed(42)
x = np.random.rand(20)
y = np.random.rand(20)
sizes = np.random.rand(20) * 1000

fig, ax = plt.subplots()
ax.scatter(x, y,
           s=sizes,
           c='coral',
           alpha=0.6,
           edgecolors='darkred',
           linewidths=2)
plt.show()

3. Multi-Group Styled

fig, ax = plt.subplots(figsize=(8, 6))

groups = {
    'A': {'x': np.random.normal(2, 0.5, 30), 'y': np.random.normal(2, 0.5, 30),
          'color': 'steelblue', 'marker': 'o'},
    'B': {'x': np.random.normal(4, 0.5, 30), 'y': np.random.normal(4, 0.5, 30),
          'color': 'coral', 'marker': 's'},
    'C': {'x': np.random.normal(3, 0.5, 30), 'y': np.random.normal(5, 0.5, 30),
          'color': 'forestgreen', 'marker': '^'}
}

for name, data in groups.items():
    ax.scatter(data['x'], data['y'], 
               c=data['color'], 
               marker=data['marker'],
               s=100, alpha=0.7, label=name)

ax.legend()
plt.show()