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

按唯一值分组绘制多条折线:筛选df中model==Hard数据绘图

实现方案

前置依赖

首先导入需要用到的工具库:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

步骤1:筛选目标样本

过滤出model列等于Hard的行,同时可以把时间列转为datetime格式避免绘图时顺序错乱:

# 筛选model为Hard的样本
hard_df = df[df['model'] == 'Hard'].copy()
# 转换时间列格式,保证横轴时间按先后顺序排列
hard_df['time'] = pd.to_datetime(hard_df['time'])

步骤2:绘制多色折线图

有两种常用实现方式,任选其一即可:

方式1:用seaborn快速实现

通过hue参数自动按不同x值分配折线颜色:

plt.figure(figsize=(10, 6))
# hue参数指定按x列区分颜色,marker加圆点标记数据点
sns.lineplot(data=hard_df, x='time', y='count', hue='x', marker='o')
# 旋转横轴标签避免重叠
plt.xticks(rotation=45)
plt.title('Hard模型下不同x值的计数随时间变化趋势')
plt.xlabel('时间')
plt.ylabel('计数')
plt.tight_layout()
plt.show()

方式2:用原生matplotlib实现

循环遍历不同的x值单独绘制折线:

plt.figure(figsize=(10,6))
for x_val in hard_df['x'].unique():
    tmp_df = hard_df[hard_df['x'] == x_val]
    plt.plot(tmp_df['time'], tmp_df['count'], label=x_val, marker='o')
plt.xticks(rotation=45)
# 显示不同x值对应的颜色图例
plt.legend()
plt.title('Hard模型下不同x值的计数随时间变化趋势')
plt.xlabel('时间')
plt.ylabel('计数')
plt.tight_layout()
plt.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 06:51:03