Modified on: 2022-08-08, 2022-09-27
Move the legend to top right corner outside of the chart, no border
ax.legend(handles, labels, =[1.05,1], loc='upper left',
bbox_to_anchor='w', facecolor='none',
edgecolor=0, borderaxespad=0) borderpad
Place the legend between two topmost horizontal gridlines
= ax.plot(...)
h
= np.mean([ax.get_yticks()[-2], ax.get_yticks()[-3]])
top_tick = ax.get_xlim()[0]
left_tick
= ax.legend(handles=[h],
l =[left_tick,top_tick], bbox_transform=ax.transData, loc='center left',
bbox_to_anchor=0.0, borderaxespad=0.5,
borderpad )