如何在折线图中用列名标注最后一点

2024-06-07 21:59:53 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here第一组代码运行良好,结果是(在注释照片之前)

fig, axes = plt.subplots(nrows=1, ncols=1)
df_Target_Relative.plot(ax=axes,x='Date',y=['CVX'],lw=2,color='black')
#df_Target_Relative.plot(ax=axes,x='Date',y=['PXD','FANG','CXO'],lw=2,color=cm_Permian)
df_Target_Relative.plot(ax=axes,x='Date',y=['US_Majors','Euro_Majors','LC_Yield','Shale_King','Global','Permian_2','Permian_Multi','Appalachia'],
                        color=cm_EVR_PALETTE,lw=2)
#df_Target_Relative.plot(ax=axes,x='Date',y=(EL_Keys),lw=1.5)
plt.rcParams["figure.figsize"] = [10,5.17] #1x1 chart
vals = axes.get_yticks()
axes.set_yticklabels(['{:,.0%}'.format(x) for x in vals])
axes.spines['right'].set_visible(False)
axes.spines['top'].set_visible(False)
axes.set_facecolor('white') #Axes facecolor
axes.grid(False)
#axes.legend(ncol=5,loc='upper center', bbox_to_anchor=(0.5, -0.15), facecolor='white')
axes.set_ylabel("Historical Share Price", labelpad=1, weight='bold', color='black')
axes.set_xlabel("Historical Date", labelpad=1, weight='bold', color='black')

这里的问题是,我的目标是用类别名称(类似于https://python-graph-gallery.com/123-highlight-a-line-in-line-plot/)注释行,但结果填充到单独的轴上

num=0
for i in df_Target_Relative.values[64][1:]:
   num+=1
   name=list(df_Target_Relative)[num]
   if name != 'CVX':
      axes.text(10.2, i, name, horizontalalignment='left', size='small', color='grey')

Tags: infalsetargetdfdateplotaxcolor

热门问题