Search code examples
pythonpandasmatplotlibseabornline-plot

How to add values/ labels over each marker in lineplot in Python Seaborn?


I have a dataframe consists of the range of time, stock and the counts of text as the columns . The dataframe looks like this

              Time Stock  Text
0   00:00 - 01:00  BBNI   371
1   00:00 - 01:00  BBRI   675
2   00:00 - 01:00  BBTN   136
3   00:00 - 01:00  BMRI   860
4   01:00 - 02:00  BBNI   936
5   01:00 - 02:00  BBRI  1325
6   01:00 - 02:00  BBTN   316
7   01:00 - 02:00  BMRI  1630

I want to draw a lineplot with the codes below :

df=tweetdatacom.groupby(["Time","Stock"])[["Text"]].count().reset_index()
plt.figure(figsize=(10,5))
line=sn.lineplot(x="Time", y="Text",hue="Stock",palette=["green","orange","red","blue"],marker="o",data=df)
plt.xticks(size=5,rotation=45, horizontalalignment='right',fontweight='light',fontsize='large')
plt.xlabel('Time Interval',size=12)
plt.ylabel('Total Tweets',size=12)

This is the result that I get with the codes : enter image description here

Now, I want to put the value for each marker on the plot, how can I do that?

Thank you very much


Solution

  • Loop through the number of data with ax.text. I'm creating this only with the data presented to me, so I'm omitting some of your processing.

    import pandas as pd
    import numpy as np
    import io
    
    data = '''
     Time Stock Text
    0 "00:00 - 01:00"  BBNI 371
    1 "00:00 - 01:00"  BBRI 675
    2 "00:00 - 01:00"  BBTN 136
    3 "00:00 - 01:00"  BMRI 860
    4 "01:00 - 02:00"  BBNI 936
    5 "01:00 - 02:00"  BBRI 1325
    6 "01:00 - 02:00"  BBTN 316
    7 "01:00 - 02:00"  BMRI 1630
    '''
    
    df = pd.read_csv(io.StringIO(data), sep='\s+')
    import seaborn as sn
    import matplotlib.pyplot as plt
    # df=tweetdatacom.groupby(["Time","Stock"])[["Text"]].count().reset_index()
    
    plt.figure(figsize=(10,5))
    
    palette = ["green","orange","red","blue"]
    line=sn.lineplot(x="Time", y="Text",hue="Stock",palette=palette, marker="o", data=df)
    
    plt.xticks(size=5,rotation=45, horizontalalignment='right',fontweight='light',fontsize='large')
    plt.xlabel('Time Interval',size=12)
    plt.ylabel('Total Tweets',size=12)
    
    for item, color in zip(df.groupby('Stock'),palette):
        for x,y,m in item[1][['Time','Text','Text']].values:
    #         print(x,y,m)
            plt.text(x,y,m,color=color)
    

    enter image description here