使用pyplot在图例上绘制最后一个值

时间:2018-06-28 15:17:43

标签: python-3.x pandas matplotlib

试图获取图例中的最新值,而我所做的任何事情都没有出现。
我可以将其显示在标题中,而不显示在当前显示“值”的图例中。这是代码...

import pandas_datareader.data as web
import datetime
import matplotlib.pyplot as plt

start = datetime.datetime(1986, 1, 1)
end = datetime.datetime(2018, 3, 1)
world_dem = web.DataReader('EIA/STEO_PATC_WORLD_A', 'quandl', start, end)
first_date = world_dem.index[-1].strftime('%Y') # -1 is first point
last_date = world_dem.index[0].strftime('%Y') #0 is last point
last_val = str(round(world_dem.Value[0],1))
fig = plt.figure(figsize=plt.figaspect(0.25))
ax = fig.add_subplot(1,1,1) #(rows,col,num)
world_dem.plot(ax=ax,grid = True, color='blue', fontsize=14,
  legend=True,marker="o") ax.set_title('Global Oil Demand ' + first_date + '- 
  ' + last_date + ' (Last Value:' + last_val + ')', fontsize=18)

ax.set_ylabel('MBPD',fontsize=14)
ax.set_xlabel('Year',fontsize=14)
ax.set_ylim([60,100])

plt.show()

先谢谢了。

1 个答案:

答案 0 :(得分:0)

尝试将label='<your value here>'传递给您的plot函数调用。