On certain LCD monitors, the color of the horizontal lines in the legend is hard to tell apart. (See the image attached). So instead of drawing a line in the legend, is it possible to just color code the text itself? so another words, have "y=0x" in blue, "y=1x" in green, etc...
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
for i in xrange(5):
ax.plot(x, i * x, label='$y = %ix$' % i)
ax.legend()
plt.show()
PS. if the line could be made thicker just in the legend, but not in the plot, this would also work.
To provide a more general solution to colorize the legend text with the color of the legend handle: This works not only for lines but any artist in a legend. It would look as follows:
This operation can be done cleanly after all plotting is done via the legend text getters/setters and axis line getters/setters. Set the legend text colors to be the same as the line colors in a for loop before plotting.
The main difference to note in this answer is that formatting the plot can be completely decoupled from the plotting operation.
Just set the
linewidth
of the legend handles:I was wondering the same thing. Here is what I came up with to change the color of the font in the legend. I am not totally happy with this method, since it seems a little clumsy, but it seems to get the job done [Edit: see below for a better way]:
2016 Edit:
Actually, there is a better way. You can simply iterate over the lines in the legend, which avoids needing to keep track of the colors as the lines are plotted. Much less clunky. Now, changing the line colors is basically a one-liner (okay, it's actually a two-liner). Here is the complete example:
2017 Edit: Lastly, if you really do want the color-coded text instead of a line (as the title suggests), then you can suppress the lines in the legend by using