杰瑞科技汇

Python pltscatter如何自定义点样式?

Of course! The plt.scatter() function from the popular matplotlib library is a fundamental tool for creating scatter plots in Python. A scatter plot is a type of plot that displays values for two variables for a set of data, using Cartesian coordinates.

Here’s a comprehensive guide covering everything from basic usage to advanced customization.


Basic Usage: The First Scatter Plot

The most basic plt.scatter() call requires two arguments: the x-coordinates and the y-coordinates of your data points.

import matplotlib.pyplot as plt
import numpy as np
# --- 1. Prepare the Data ---
# Generate some sample data using NumPy
# x will be an array of 50 random numbers between 0 and 100
x = np.random.rand(50) * 100
# y will be an array of 50 random numbers between 0 and 100
y = np.random.rand(50) * 100
# --- 2. Create the Scatter Plot ---
# Call plt.scatter with x and y data
plt.scatter(x, y)
# --- 3. Display the Plot ---
plt.show()

What this code does:

  1. import matplotlib.pyplot as plt: Imports the necessary library, using the conventional alias plt.
  2. import numpy as np: Imports NumPy for easy numerical array generation.
  3. plt.scatter(x, y): This is the core function. It takes the x and y arrays and creates a scatter plot where each point's position is determined by the corresponding values in x and y.
  4. plt.show(): This command opens a window and displays the plot you've created.

Customizing the Scatter Plot

A basic plot is good, but customization is what makes plt.scatter() powerful. You can change the size, color, and marker of the points.

A. Changing Point Size (s parameter)

You can make all points the same size by passing a single number, or give each point a unique size by passing an array of sizes.

import matplotlib.pyplot as plt
import numpy as np
# Prepare data
x = np.random.rand(50) * 100
y = np.random.rand(50) * 100
# Create an array of sizes for each point (e.g., size based on the x-value)
sizes = x * 2 # Scale the x-values to make them more visible
# Create the scatter plot with custom sizes
plt.scatter(x, y, s=sizes)
# Add labels and a title for clarity
plt.xlabel('X-axis Data')
plt.ylabel('Y-axis Data')'Scatter Plot with Custom Point Sizes')
plt.show()

B. Changing Point Color (c parameter)

Similar to size, you can set a single color for all points or use an array of values to create a color map.

import matplotlib.pyplot as plt
import numpy as np
# Prepare data
x = np.random.rand(50) * 100
y = np.random.rand(50) * 100
# Create an array of colors for each point (e.g., color based on the y-value)
colors = y
# Create the scatter plot with custom colors
# The `cmap` argument specifies a colormap (e.g., 'viridis', 'plasma', 'coolwarm')
plt.scatter(x, y, c=colors, cmap='viridis')
# Add a color bar to show the mapping from values to colors
plt.colorbar(label='Color based on Y-value')
# Add labels and a title
plt.xlabel('X-axis Data')
plt.ylabel('Y-axis Data')'Scatter Plot with a Color Map')
plt.show()

C. Changing Point Marker (marker parameter)

You can change the shape of the markers using the marker argument.

import matplotlib.pyplot as plt
import numpy as np
# Prepare data
x = np.random.rand(50) * 100
y = np.random.rand(50) * 100
# Create the scatter plot with a different marker style
# Other options: 's' (square), '^' (triangle up), 'v' (triangle down), 'd' (diamond)
plt.scatter(x, y, marker='x', s=50)
# Add labels and a title
plt.xlabel('X-axis Data')
plt.ylabel('Y-axis Data')"Scatter Plot with 'x' Markers")
plt.show()

Combining Customizations

You can combine all these parameters in a single plot to create rich, informative visualizations.

import matplotlib.pyplot as plt
import numpy as np
# --- Prepare Data ---
# Let's create data that has some relationship
np.random.seed(42) # for reproducible results
x = np.random.rand(50) * 100
y = 2.5 * x + np.random.randn(50) * 20 # y is roughly 2.5*x with some noise
# --- Customization Parameters ---
# Size of points based on their x-value
sizes = x * 1.5
# Color of points based on their y-value
colors = y
# --- Create the Final, Customized Scatter Plot ---
scatter = plt.scatter(
    x, 
    y, 
    s=sizes,         # Set size
    c=colors,        # Set color values
    cmap='plasma',   # Choose a colormap
    alpha=0.7,       # Set transparency (0=transparent, 1=opaque)
    edgecolors='w',  # White edge color for points
    linewidths=0.5   # Width of the edge lines
)
# --- Add Final Touches ---
# Add a color bar
cbar = plt.colorbar(scatter)
cbar.set_label('Y-axis Value (Color Scale)')
# Add labels and a title
plt.xlabel('Independent Variable (X)')
plt.ylabel('Dependent Variable (Y)')'Advanced Scatter Plot: Size, Color, and Transparency')
# Add a grid
plt.grid(True, linestyle='--', alpha=0.6)
plt.show()

Plotting Multiple Datasets on One Axes

You can call plt.scatter() multiple times on the same plot to compare different datasets.

import matplotlib.pyplot as plt
import numpy as np
# --- Data for Group A ---
x_a = np.random.rand(30) * 100
y_a = np.random.rand(30) * 100
# --- Data for Group B ---
x_b = np.random.rand(40) * 100
y_b = np.random.rand(40) * 100 + 30 # Shifted up
# --- Plot both datasets ---
# Plot Group A
plt.scatter(x_a, y_a, color='blue', label='Group A', alpha=0.7)
# Plot Group B
plt.scatter(x_b, y_b, color='red', label='Group B', alpha=0.7)
# --- Add Final Touches ---
# Add a legend to distinguish the groups
plt.legend()
# Add labels and a title
plt.xlabel('X-axis Data')
plt.ylabel('Y-axis Data')'Comparing Two Groups with a Scatter Plot')
plt.show()

plt.scatter() vs. plt.plot()

A common point of confusion is when to use plt.scatter() versus plt.plot().

Feature plt.scatter() plt.plot()
Primary Use Visualizing the relationship between two variables. Plotting y-values against x-values (e.g., time series, functions).
Performance Slower for large datasets, as each point is a separate object. Much faster for large datasets, as it creates a single line object.
Customization Excellent. Each point can have a unique size, color, and marker. Limited. The entire line/series has the same color, size, and style.
Marker Style The marker argument controls the shape of individual points. The style string (e.g., 'ro-') can set marker (o), color (r), and line () styles.

Rule of Thumb:

  • Use plt.scatter() when you want to highlight individual data points and customize them individually (e.g., showing a third dimension with size/color).
  • Use plt.plot() when you are plotting a continuous function or connecting points in a series, especially with many data points.
# Example of plt.plot() for a line
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y, 'g--') # Green, dashed line'Using plt.plot() for a Function')
plt.show()

Summary of Key Parameters

Parameter Description Example
x, y The data for the x and y coordinates. plt.scatter(x_data, y_data)
s The size of markers. Can be a scalar or an array. s=50 or s=sizes_array
c The color of markers. Can be a color name, array of colors, or array of values for a colormap. c='red' or c=color_values
cmap The colormap to use when c is an array of numerical values. cmap='viridis'
alpha The alpha transparency of the markers (0.0 to 1.0). alpha=0.5
marker The style of the marker. marker='s' (square)
label The label for the dataset, used in the legend. label='Experiment 1'
edgecolors The color of the marker edge. edgecolors='black'
linewidths The width of the marker edge. linewidths=1
分享:
扫描分享到社交APP
上一篇
下一篇