Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add column labels to graphs

I was wondering, if you can annotate every graph in this example automatically using the column headers as labels.

    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

When I use this code, I only get the column headers as a legend. However, I want the labels to be directly beside/above my graphs.

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Is this what you are looking for?

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')

output:

annotated graph

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading