pythonjupyter-notebooklinemarkerline-plot

Markers in beginning and end of line plots


I have 5 datasets that have thousands of x and y coordinates grouped by 'frame' that create 5 trajectory plots. I'd like to mark the first and last coordinates for each plot but having difficulty figuring it out. I am using Jupiter Notebook.

mean_pos1 = gr1.mean()
mean_pos2 = gr2.mean()
mean_pos3 = gr3.mean()
mean_pos4 = gr4.mean()
mean_pos5 = gr5.mean()
plt.figure()

xlim=(200, 1500)
ylim=(0, 1200)
ax1 = mean_pos1.plot(x='x', y='y',color='blue',label='Dolphin A'); ax1.set_title('mean trajectory'); 
ax2 = mean_pos2.plot(x='x', y='y',color='red',label='Dolphin B'); ax2.set_title('mean trajectory');
ax3 = mean_pos3.plot(x='x', y='y',color='green',label='Dolphin C'); ax3.set_title('mean trajectory');
ax4 = mean_pos4.plot(x='x', y='y',color='magenta',label='Dolphin D'); ax4.set_title('mean trajectory');
ax5 = mean_pos5.plot(x='x', y='y',color='cyan',label='Dolphin E'); ax5.set_title('mean trajectory');
ax1.set_xlim(xlim)
ax1.set_ylim(ylim)
ax2.set_xlim(xlim)
ax2.set_ylim(ylim)
ax3.set_xlim(xlim)
ax3.set_ylim(ylim)
ax4.set_xlim(xlim)
ax4.set_ylim(ylim)
ax5.set_xlim(xlim)
ax5.set_ylim(ylim)


plt.show()

the output of them looks like this:

line_trajectory


Solution

  • Use the scatter method to plot the markers separately on the same axis by grabbing the first and last elements from your x and y series:

    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd
    
    df = pd.DataFrame({'x': np.random.normal(3,0.2,10), 'y': np.random.normal(5,0.3,10)})
    
    fig, ax = plt.subplots()
    
    df.plot(x='x', y='y', ax=ax)
    
    ax.scatter(df['x'].iloc[0], df['y'].iloc[0], marker='o', color='red')
    ax.scatter(df['x'].iloc[-1], df['y'].iloc[-1], marker='o', color='red')
    
    plt.show()
    

    line plot with endpoint markers