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

如何优化Seaborn lineplot的数值标注效果?

折线图标注优化问题与解决方案

数据集

mentor_cntmentee_cnt
033
133
277
31819
42427
53735
65356
76370
88689
9102114
10135149
11154169
12174202
13232287
14298386
15343475
16384552
17446684
18509883
19469757

初始实现代码

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('darkgrid')

fig, ax = plt.subplots(figsize=(10, 6))
sns.lineplot(x=df['month'], y=df['mentor_cnt'], color = 'b', label='Mentors', markers=True, marker='o')
sns.lineplot(x=df['month'], y=df['mentee_cnt'], color = 'r', label='Mentee', markers=True, marker='o')
plt.ylim(min(df['mentee_cnt'])-60, max(df['mentee_cnt'])+50)
columns =['mentor_cnt', 'mentee_cnt']
colors = ['b', 'w']
facecolors = ['none', 'r']
x_position = [0, -10]
y_position = [-18, 5]

for col, fc, c, x_p, y_p in zip(columns, facecolors, colors, x_position, y_position):
    for x, y in zip(df['month'], df[col]):
        label = '{:.0f}'.format(y)
        plt.annotate(
            label,
            (x, y),
            textcoords='offset points',
            xytext=(x_p, y_p),
            ha='center',
            color=c
        ).set_bbox(dict(facecolor=fc, alpha=0.5, boxstyle='round', edgecolor='none'))
ax.legend()
plt.xlabel('Month')
plt.ylabel('Number of persons')
plt.title('Mentee, mentor')
plt.show()

初始效果

导师(Mentors)的折线标注效果尚可,但学员(Mentee)的数值标注拥挤重叠,可读性差,尝试用set_bbox提升效果也不佳。

优化思路

通过仅标注关键分位数和末尾数值减少标注数量,避免拥挤,同时调整标注位置参数提升可读性。

优化后代码

fig, ax = plt.subplots(figsize=(10, 6))
sns.lineplot(x=df['month'], y=df['mentor_cnt'], color='b', label='Mentors', markers=True, marker='o')
sns.lineplot(x=df['month'], y=df['mentee_cnt'], color='r', label='Mentee', markers=True, marker='o')
plt.ylim(min(df['mentee_cnt'])-60, max(df['mentee_cnt'])+50)
columns = ['mentor_cnt', 'mentee_cnt']
colors = ['b', 'r']
facecolors = ['none', 'r']
x_position = [0, -15]
y_position = [-18, 3]

for col, fc, c, x_p, y_p in zip(columns, facecolors, colors, x_position, y_position):
    for x, y in zip(df['month'], df[col]):
        # 生成包含分位数和末尾值的关键数值列表
        lst = [0, 0.25, 0.5, 0.75, 1]
        describe_nearest = []
        [describe_nearest.append(df[col].quantile(el, interpolation='nearest')) for el in lst]
        describe_nearest.append(df[col].values[-1::][0])
        # 仅对关键数值添加标注
        if y in describe_nearest:
            label = '{:.0f}'.format(y)
            plt.annotate(
                label,
                (x, y),
                textcoords='offset points',
                xytext=(x_p, y_p),
                ha='center',
                color=c
            )
ax.legend()
plt.xlabel('Month')
plt.ylabel('Number of persons')
plt.title('Mentee, mentor')
plt.show()

优化后效果

仅保留关键分位数(0、25%、50%、75%、100%分位)和最后一个数值的标注,标注布局清晰,学员折线的标注可读性显著提升。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 07:57:31