Matplotlib 散点图
最后修改于 2025 年 2 月 25 日
Matplotlib 是一个强大的 Python 库,用于创建静态、动态和交互式可视化效果。 散点图用于可视化两个变量之间的关系。 本教程介绍如何使用 Matplotlib 创建各种类型的散点图。
散点图非常适合识别数据中的趋势、相关性和异常值。 Matplotlib 提供了一个灵活且易于使用的界面,用于创建具有自定义功能的散点图。
基本散点图
此示例演示如何创建基本散点图。
basic_scatter_chart.py
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
# Create a scatter chart
plt.scatter(x, y)
# Add labels and title
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Basic Scatter Chart")
# Display the chart
plt.show()
plt.scatter() 函数用于创建散点图。 plt.show() 函数显示图表。
自定义散点图
此示例演示如何使用颜色、大小和标记自定义散点图。
custom_scatter_chart.py
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
sizes = [100, 200, 300, 400, 500] # Marker sizes
colors = ['red', 'green', 'blue', 'purple', 'orange'] # Marker colors
# Create a scatter chart with custom styles
plt.scatter(x, y, s=sizes, c=colors, alpha=0.6, edgecolors="black")
# Add labels and title
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Custom Scatter Chart")
# Display the chart
plt.show()
s、c、alpha 和 edgecolors 参数用于自定义标记的外观。
带有颜色映射的散点图
此示例演示如何使用颜色映射来表示第三个变量。
color_mapping_scatter.py
import matplotlib.pyplot as plt
import numpy as np
# Data
x = np.random.rand(50)
y = np.random.rand(50)
colors = np.random.rand(50) # Third variable for color mapping
sizes = 1000 * np.random.rand(50) # Third variable for size mapping
# Create a scatter chart with color mapping
plt.scatter(x, y, c=colors, s=sizes, alpha=0.6, cmap="viridis")
# Add a colorbar
plt.colorbar()
# Add labels and title
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Scatter Chart with Color Mapping")
# Display the chart
plt.show()
cmap 参数用于将颜色映射应用于标记。 plt.colorbar() 函数向图表添加颜色条。
带有回归线的散点图
此示例演示如何向散点图添加回归线。
scatter_with_regression.py
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import linregress
# Data
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
# Create a scatter chart
plt.scatter(x, y)
# Add a regression line
slope, intercept, r_value, p_value, std_err = linregress(x, y)
plt.plot(x, slope * np.array(x) + intercept, color="red", label="Regression Line")
# Add labels, title, and legend
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Scatter Chart with Regression Line")
plt.legend()
# Display the chart
plt.show()
scipy.stats 中的 linregress() 函数用于计算回归线。 plt.plot() 函数将回归线添加到图表中。
3D 散点图
此示例演示如何创建 3D 散点图。
3d_scatter_chart.py
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# Data
x = np.random.rand(50)
y = np.random.rand(50)
z = np.random.rand(50)
# Create a 3D scatter chart
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ax.scatter(x, y, z)
# Add labels and title
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.set_zlabel("Z-axis")
ax.set_title("3D Scatter Chart")
# Display the chart
plt.show()
mpl_toolkits.mplot3d 模块用于创建 3D 散点图。 projection="3d" 参数启用 3D 绘图。
散点图的最佳实践
- 清晰标注轴:始终为 X 轴和 Y 轴添加标签,以使图表易于理解。
- 使用颜色映射: 使用颜色映射有效地表示第三个变量。
- 选择合适的标记: 使用易于区分和解释的标记。
- 限制数据点:避免用过多的数据点使图表混乱。
来源
在本文中,我们探讨了使用 Matplotlib 的各种类型的散点图,包括基本散点图、自定义散点图、颜色映射散点图、回归散点图和 3D 散点图。
作者
列出 所有 Python 教程。