You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Matplotlib图表中为不同线条添加不同标记

解决方案

方案1:直接给marker参数传入标记序列

pandas的DataFrame.plot.line的marker参数支持传入和DataFrame列数等长的标记列表,每个元素对应一条曲线的标记样式,是最简单的实现方式。
示例代码(适配4条曲线的场景,你可以按需调整标记样式):

import matplotlib.pyplot as plt

# 顺序对应df的列顺序,可选标记包括o(圆圈)、x(叉)、s(正方形)、^ (上三角)、*(星号)等
markers = ['o', 'x', 's', '^']
df.plot.line(marker=markers)
plt.xlabel('System Size')
plt.ylabel('Updated Cells')
plt.show()

方案2:遍历线对象精细化配置

如果需要同时调整标记大小、边线颜色等个性化属性,可以拿到绘图返回的轴对象,遍历所有线条单独设置参数:

import matplotlib.pyplot as plt

ax = df.plot.line()
line_markers = ['o', 'x', 's', '^']
for line, marker in zip(ax.lines, line_markers):
    line.set_marker(marker)
    # 额外配置示例:设置标记大小
    # line.set_markersize(7)
    # 额外配置示例:设置标记填充颜色
    # line.set_markerfacecolor('white')

plt.xlabel('System Size')
plt.ylabel('Updated Cells')
plt.show()

内容的提问来源于stack exchange,提问作者MRL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 02:06:05