I have data that results in multiple lines being plotted, I want to give these lines a single label in my legend. I think this can be better demonstrated using the example b
Numpy solution based on will's response above.
import numpy as np
import matplotlib.pylab as plt
a = np.array([[3.57, 1.76, 7.42, 6.52],
[1.57, 1.20, 3.02, 6.88],
[2.23, 4.86, 5.12, 2.81],
[4.48, 1.38, 2.14, 0.86],
[6.68, 1.72, 8.56, 3.23]])
plt.plot(a[:,::2].T, a[:, 1::2].T, 'r', label='data_a')
handles, labels = plt.gca().get_legend_handles_labels()
Assuming that equal labels have equal handles, get unique labels and their respective indices, which correspond to handle indices.
labels, ids = np.unique(labels, return_index=True)
handles = [handles[i] for i in ids]
plt.legend(handles, labels, loc='best')
plt.show()