matplotlib remove duplicate legend entries from plotting loop

43

def legend_without_duplicate_labels(ax):
    handles, labels = axes.get_legend_handles_labels()
    unique = [(h, l) for i, (h, l) in enumerate(zip(handles, labels)) if l not in labels[:i]]
    axes.legend(*zip(*unique))
legend_without_duplicate_labels(axes)
    
plt.show(); plt.close()

Comments

Submit
0 Comments