x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
colors = []
for i in range(5):
    line, = ax.plot(x, i * x, label='$y = %ix$' % i)
colors.append(plt.getp(line, 'color'))
leg = ax.legend()
for color, text in zip(colors, leg.get_texts()):
    text.set_color(color)
plt.show()
Matplotlib: text color code in the legend instead of a line