Matplotlib 绘图标记(Marker)完全指南
在 matplotlib.pyplot.plot() 或 scatter() 中,标记(Marker) 是数据点的可视化符号(如圆点、方块、星形等)。掌握标记样式能让你的图表更专业、更具表现力。
一、基本语法
plt.plot(x, y, marker='o') # 指定标记样式
plt.scatter(x, y, marker='s') # 散点图也适用
marker=参数控制 数据点的形状
二、常用内置标记(推荐)
| 标记符号 | 名称 | 示例代码 |
|---|---|---|
'o' | 实心圆 | marker='o' |
'.' | 小点 | marker='.' |
',' | 像素点 | marker=',' |
's' | 正方形 | marker='s' |
'p' | 五边形 | marker='p' |
'*' | 星形 | marker='*' |
'h' | 六边形1 | marker='h' |
'H' | 六边形2 | marker='H' |
'+' | 加号 | marker='+' |
'x' | 叉号 | marker='x' |
'D' | 菱形 | marker='D' |
'd' | 细菱形 | marker='d' |
'|' | 垂直线 | marker='|' |
'_' | 水平线 | marker='_' |
'^' | 上三角 | marker='^' |
'v' | 下三角 | marker='v' |
'<' | 左三角 | marker='<' |
'>' | 右三角 | marker='>' |
'1', '2', '3', '4' | 三脚架样式 | marker='1' |
三、标记样式参数(精细控制)
plt.plot(x, y,
marker='o', # 标记形状
markersize=10, # 标记大小
markerfacecolor='red', # 标记填充色
markeredgecolor='black', # 标记边框色
markeredgewidth=2, # 边框宽度
alpha=0.7) # 透明度
注意:
scatter()支持更多参数(如s=控制大小)
四、格式字符串快速设置(MATLAB 风格)
plt.plot(x, y, 'ro--')
# 含义:红色(r) + 圆点(o) + 虚线(--)
| 位置 | 含义 |
|---|---|
| 颜色 | r, g, b, c, m, y, k, w |
| 标记 | o, ., s, *, +, x, D 等 |
| 线型 | - 实线, -- 虚线, : 点线, -. 点划线 |
plt.plot(x, y, 'g^-.') # 绿色上三角 + 点划线
五、完整示例:展示所有标记
import matplotlib.pyplot as plt
import numpy as np
# 所有标记列表
markers = ['.', ',', 'o', 'v', '^', '<', '>', '1', '2', '3', '4',
'8', 's', 'p', '*', 'h', 'H', '+', 'x', 'D', 'd', '|', '_']
# 生成示例数据
x = np.arange(len(markers))
y = np.ones_like(x)
plt.figure(figsize=(14, 6))
for i, marker in enumerate(markers):
plt.scatter(x[i], y[i], marker=marker, s=200, label=f"'{marker}'")
plt.xlim(-1, len(markers))
plt.ylim(0.8, 1.2)
plt.xticks(x, markers)
plt.title('Matplotlib 所有内置标记样式', fontsize=16)
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
plt.tight_layout()
plt.show()
六、进阶:自定义标记(路径 Path)
from matplotlib.path import Path
import matplotlib.patches as patches
# 自定义五角星
verts = [
(0., 0.), (0.2, 0.3), (0.5, 0.3), (0.3, 0.5), (0.4, 0.8),
(0., 0.6), (-0.4, 0.8), (-0.3, 0.5), (-0.5, 0.3), (-0.2, 0.3),
(0., 0.)
]
codes = [Path.MOVETO] + [Path.LINETO]*9 + [Path.CLOSEPOLY]
path = Path(verts, codes)
patch = patches.PathPatch(path, facecolor='gold', edgecolor='black')
fig, ax = plt.subplots()
ax.add_patch(patch)
ax.set_xlim(-0.6, 0.6)
ax.set_ylim(0, 1)
ax.axis('off')
plt.show()
七、常用组合推荐(美观专业)
| 用途 | 推荐标记 |
|---|---|
| 科学论文 | 'o', 's', '^', 'D' |
| 演示图表 | '*'(醒目), 'p'(五边形) |
| 大数据散点 | '.' 或 ','(轻量) |
| 强调重点 | marker='*', markersize=12, markerfacecolor='yellow', markeredgecolor='red' |
# 专业风格示例
plt.plot(x, y, 'o', markersize=8, markerfacecolor='white',
markeredgecolor='blue', markeredgewidth=2, label='数据点')
八、标记 + 线条组合技巧
# 每隔 n 个点显示标记
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y, '-', color='gray', alpha=0.5) # 浅色线
plt.plot(x[::10], y[::10], 'o', markersize=8,
markerfacecolor='red', markeredgecolor='black', label='关键点')
plt.legend()
plt.show()
九、Scatter 专属:大小 + 颜色映射
plt.scatter(x, y,
s=np.abs(y)*100, # 标记大小随 y 变化
c=y, cmap='coolwarm', # 颜色映射
alpha=0.7, edgecolors='k')
plt.colorbar(label='sin(x)')
plt.show()
十、快速参考表(打印/收藏用)
MARKER_STYLES = {
'point': ',',
'pixel': '.',
'circle': 'o',
'triangle_down': 'v',
'triangle_up': '^',
'square': 's',
'pentagon': 'p',
'star': '*',
'hexagon1': 'h',
'hexagon2': 'H',
'plus': '+',
'x': 'x',
'diamond': 'D',
'thin_diamond': 'd',
'vline': '|',
'hline': '_'
}
官方文档
- 标记参考:https://matplotlib.org/stable/api/markers_api.html
- 示例:https://matplotlib.org/stable/gallery/lines_bars_and_markers/marker_reference.html
总结:一图胜千言
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 20)
y = np.sin(x)
plt.plot(x, y, 'o-',
markersize=10,
markerfacecolor='lightblue',
markeredgecolor='navy',
markeredgewidth=2,
linewidth=2,
label='sin(x)')
plt.title('完美标记样式示例', fontsize=14)
plt.xlabel('X 轴')
plt.ylabel('Y 轴')
plt.grid(True, alpha=0.3)
plt.legend()
plt.show()
现在就试试! 复制下面代码,换不同 marker 看看效果:
plt.plot([1,2,3,4], [1,4,2,3], marker='*', markersize=15, markerfacecolor='gold')
plt.show()
需要我为你:
- 生成 所有标记的对比图 PNG?
- 制作 可自定义的标记选择器工具?
- 输出 LaTeX/PPT 可用高清标记表?
告诉我你的需求!