如何向图表添加列标签

How to add column labels to graphs

我想知道,您是否可以使用列 headers 作为标签自动注释此示例中的每个图表。

    import seaborn as sns
    import pandas as pd
    
    d = {'a': [100, 125, 300, 520],..., 'z': [250, 270, 278, 248]}
    df = pd.DataFrame(data=d, index=[25, 26, 26, 30])
             a    ...     z
  25        100   ...    250
  26        125   ...    270
  26        300   ...    278
  30        520   ...    248

当我使用这段代码时,我只得到 headers 列作为图例。但是,我希望标签直接 beside/above 我的图表。

sns.lineplot(data=df, dashes=False, estimator=None)

类似于:

ax = sns.lineplot(data=df, dashes=False, estimator=None)

for c, l in zip(df.columns, ax.lines):
    y = l.get_ydata()
    ax.annotate(f'{c}', xy=(1.01,y[-1]), xycoords=('axes fraction', 'data'), 
                ha='left', va='center', color=l.get_color())

来源:

这是您要找的吗?

ax = sns.lineplot(data=df, dashes=False, estimator=None, legend=False)

for label, pos in df.iloc[0].iteritems():
    ax.annotate(label, (df.index[0], pos*1.05), ha='left', va='bottom')

输出: