Answers for "matplotlib remove duplicate legend entries from plotting loop"

0

matplotlib remove duplicate legend entries from plotting loop

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()
Posted by: Guest on March-02-2022

Code answers related to "matplotlib remove duplicate legend entries from plotting loop"

Python Answers by Framework

Browse Popular Code Answers by Language