Matplotlib Ref Scatter
* * Matplotlib Reference](#)
`scatter()` is used to draw scatter plots, where each data point can be independently configured with size, color, and transparency.
Unlike `plot()`, `scatter()` allows each point to have different appearance properties, making it suitable for displaying the distribution relationships of three-dimensional or four-dimensional data.
## Function Definition
### pyplot Interface
matplotlib.pyplot.scatter(x, y, s=None, c=None, marker='o', cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, edgecolors=None, **kwargs)
### Axes Interface
Axes.scatter(x, y, s=None, c=None, marker='o', cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, edgecolors=None, **kwargs)
## Parameter Description
| Parameter | Type | Description |
| --- | --- | --- |
| x, y | array-like | x and y coordinates of data points (required) |
| s | float or array-like | Point size, can be a scalar (same for all points) or an array (different size for each point) |
| c | color or array-like | Point color, can be a fixed color or a numeric array (used with cmap to map colors) |
| marker | str | Marker shape: 'o' (circle), 's' (square), '^' (up triangle), 'v' (down triangle), 'D' (diamond), '*' (star), '+' (plus), 'x' (cross), etc., default 'o' |
| cmap | str or Colormap | Color mapping, used when c is a numeric array, such as 'viridis', 'plasma' |
| norm | Normalize | Normalization method from data to color mapping |
| vmin, vmax | float | Data range for color mapping, points outside the range will be clipped |
| alpha | float or array-like | Transparency, between 0-1 |
| linewidths | float or array-like | Point edge line width |
| edgecolors | color or array-like | Point edge color, 'face' uses fill color, 'none' for no edge |
| label | str | Legend label |
> The core advantage of `scatter()` is that `s`, `c`, and `alpha` can be arrays with the same length as x/y, allowing a single scatter plot to simultaneously display 3-4 dimensions of data information.
* * *
## Usage Examples
### Example 1: Basic Scatter Plot
## Example
import matplotlib.pyplot as plt
import numpy as np
# Generate random data
np.random.seed(42)
x = np.random.rand(50)
y = np.random.rand(50)
fig, ax = plt.subplots(layout='constrained')
ax.scatter(x, y, color='steelblue', edgecolors='white',
linewidth=0.5, s=80)
ax.set_title('Basic Scatter Plot')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.grid(True, alpha=0.3)
plt.show()
### Example 2: Varying Size and Color (Displaying Multi-dimensional Data)
## Example
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
n =100
# Four dimensions of data
x = np.random.rand(n) * 10# Dimension 1: x coordinate
y = np.random.rand(n) * 10# Dimension 2: y coordinate
colors = np.random.rand(n) * 100# Dimension 3: color (numeric value)
sizes = np.random.rand(n) * 300# Dimension 4: size
alpha_values = np.random.rand(n) * 0.7 + 0.3# Dimension 5: transparency
fig, ax = plt.subplots(figsize=(8,6), layout='constrained')
scatter = ax.scatter(x, y,
c=colors,# color varies with data
s=sizes,# size varies with data
alpha=0.6,# fixed transparency
cmap='viridis',# color mapping
edgecolors='white',
linewidth=0.5)
# Add color bar
cbar = fig.colorbar(scatter, ax=ax)
cbar.set_label('Color Value')
ax.set_title('Multi-dimensional Scatter Plot')
ax.set_xlabel('X Coordinate')
ax.set_ylabel('Y Coordinate')
ax.grid(True, alpha=0.2)
plt.show()
### Example 3: Multi-group Scatter Comparison
## Example
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
# Generate three groups of data with different distributions
group1_x = np.random.normal(2,0.5,100)
group1_y = np.random.normal(2,0.5,100)
group2_x = np.random.normal(6,0.5,100)
group2_y = np.random.normal(6,0.5,100)
group3_x = np.random.normal(4,1.0,100)
group3_y = np.random.normal(4,1.0,100)
fig, ax = plt.subplots(figsize=(8,6), layout='constrained')
# Use different colors and markers for each group
ax.scatter(group1_x, group1_y, c='#e74c3c', marker='o',
s=60, label='Group A (tight cluster)')
ax.scatter(group2_x, group2_y, c='#2ecc71', marker='s',
s=60, label='Group B (tight cluster)')
ax.scatter(group3_x, group3_y, c='#3498db', marker='^',
s=60, label='Group C (spread cluster)')
ax.set_title('Multi-group Scatter Comparison')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.legend()
ax.grid(True, alpha=0.2)
plt.show()
### Example 4: Scatter Plot with Color Bar (Bubble Chart)
## Example
import matplotlib.pyplot as plt
import numpy as np
# Simulated data: population, GDP, and region for different cities
np.random.seed(42)
n =30
population = np.random.randint(50,500, n)# Population (determines bubble size)
gdp = np.random.randint(100,1000, n)# GDP (determines color depth)
region = np.random.choice(['North','South','East','West'], n)
x = np.random.rand(n) * 100
y = np.random.rand(n) * 100
fig, ax = plt.subplots(figsize=(9,6), layout='constrained')
scatter = ax.scatter(x, y,
s=population,# bubble size = population
c=gdp,# bubble color = GDP
cmap='YlOrRd',# yellow-orange-red color mapping
alpha=0.7,
edgecolors='gray',
linewidth=0.5)
ax.set_title('Bubble Chart: City Population vs GDP')
ax.set_xlabel('X Coordinate')
ax.set_ylabel('Y Coordinate')
# Add color bar, labeled as GDP
cbar = fig.colorbar(scatter, ax=ax, label='GDP')
ax.grid(True, alpha=0.2)
plt.show()
* * *
## FAQ
### What is the difference between scatter() and plot()?
`plot()` is suitable for line charts, where all points share the same color and marker style, with high rendering efficiency.
`scatter()` is suitable for scatter plots, where each point can be independently configured with size and color, but each point is an independent Artist, making it slower with large amounts of data.
When there are many data points (tens of thousands or more), consider using `plot()` with a specified marker to improve performance.
### What is the unit of the s parameter?
`s` represents the area of the point (in units of square points, points^2), not the diameter.
For example, `s=100` and `s=400`, the latter has 4 times the area of the former, and the diameter is approximately 2 times that of the former.
[ Matplotlib Reference](#)
YouTip