Sometimes you need a specific order of your labels in your matplotlib legend.
You automatically will have an order of line1, line2, line3, scatter1, scatter2:

But let’s say you need an order of scatter1, line1, scatter 2, ..
The following snipped is an easy fix and very useful:
handles, labels = ax.get_legend_handles_labels()
order = [3,0,4,1,2]
plt.legend([handles[idx] for idx in order],[labels[idx] for idx in order],fontsize=12)
