Show the final y-axis value of each line with matplotlib

前端 未结 3 636
旧巷少年郎
旧巷少年郎 2020-12-11 04:57

I\'m drawing a graph with some lines using matplotlib and I want to display the final y value next to where each line ends on the right hand side like this:

3条回答
  •  一整个雨季
    2020-12-11 05:57

    Very useful Joe. Only one more detail. If the final value isn't a maximun, you can use y[-1]. I added a horizontal line to clarify.

    gbm = np.log(np.cumsum(np.random.randn(10000))+10000)
    plt.plot(gbm)
    plt.annotate('%0.2f' % gbm[-1], xy=(1, gbm[-1]), xytext=(8, 0), 
                 xycoords=('axes fraction', 'data'), textcoords='offset points')
    plt.axhline(y=gbm[-1], color='y', linestyle='-.')
    plt.show()
    

    Plot with final y-axis value marked.

提交回复
热议问题