I am having trouble with aligning my legend labels in a subplot in matplotlib. I have tried a workaround which involved padding the labels via the ljust function, however for some reason it does not transfer well over to the legend itself. Below is some example code with generated data to replicate my issue.
I am trying to have the label text aligned like the printed statements.
labellist = ['LR','XGB','SVM','LASSO']
fig, axs = plt.subplots(1, 1,figsize=(10,5))
fig.suptitle('ROC curves of the ML algorithms')
axs.set_title('ROC curve test set')
axs.set(xlabel="1 - Specificity", ylabel='Sensitivity')
axs.axis([-0.005, 1, 0, 1.005])
for i,item in enumerate(labellist):
x = np.linspace(0, np.pi, 100)
item_label = item.ljust(5) +" (auc: " +str(i)+ ')'
axs.plot(x, np.cos(x*i), linewidth=1.5, label=item_label)
print(item_label)
axs.legend()
Does anybody have a solution to this?