How to annotate end of lines using python and matplotlib?

前端 未结 2 2088
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 12:10

With a dataframe and basic plot such as this:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(123456)
rows = 75
df = p         


        
相关标签:
2条回答
  • 2020-11-28 12:48

    In order to annotate a point use ax.annotate(). In this case it makes sense to specify the coordinates to annotate separately. I.e. the y coordinate is the data coordinate of the last point of the line (which you can get from line.get_ydata()[-1]) while the x coordinate is independent of the data and should be the right hand side of the axes (i.e. 1 in axes coordinates). You may then also want to offset the text a bit such that it does not overlap with the axes.

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    rows = 75
    df = pd.DataFrame(np.random.randint(-4,5,size=(rows, 3)), columns=['A', 'B', 'C'])
    datelist = pd.date_range(pd.datetime(2017, 1, 1).strftime('%Y-%m-%d'), periods=rows).tolist()
    df['dates'] = datelist 
    df = df.set_index(['dates'])
    df.index = pd.to_datetime(df.index)
    df = df.cumsum()
    
    ax = df.plot()
    
    for line, name in zip(ax.lines, df.columns):
        y = line.get_ydata()[-1]
        ax.annotate(name, xy=(1,y), xytext=(6,0), color=line.get_color(), 
                    xycoords = ax.get_yaxis_transform(), textcoords="offset points",
                    size=14, va="center")
    
    plt.show()
    

    0 讨论(0)
  • 2020-11-28 12:52

    Method 1

    Here is one way, or at least a method, which you can adapt to aesthetically fit in whatever way you want, using the plt.annotate method:

    [EDIT]: If you're going to use a method like this first one, the method outlined in ImportanceOfBeingErnest's answer is better than what I've proposed.

    df.plot()
    
    for col in df.columns:
        plt.annotate(col,xy=(plt.xticks()[0][-1]+0.7, df[col].iloc[-1]))
    
    plt.show()
    

    For the xy argument, which is the x and y coordinates of the text, I chose the last x coordinate in plt.xticks(), and added 0.7 so that it is outside of your x axis, but you can coose to make it closer or further as you see fit.

    METHOD 2:

    You could also just use the right y axis, and label it with your 3 lines. For example:

    fig, ax = plt.subplots()
    df.plot(ax=ax)
    ax2 = ax.twinx()
    ax2.set_ylim(ax.get_ylim())
    ax2.set_yticks([df[col].iloc[-1] for col in df.columns])
    ax2.set_yticklabels(df.columns)
    
    plt.show()
    

    This gives you the following plot:

    0 讨论(0)
提交回复
热议问题