Matplotlib: Color-coded text in legend instead of a line

legend, matplotlib, numpy, plot, python

Solution

Just set the `linewidth` of the legend handles:

In [55]: fig, ax = plt.subplots()

In [56]: x = np.arange(10)

In [57]: for i in xrange(5):                    
   ....:     ax.plot(x, i * x, label='$y = %ix$' % i)
   ....:     

In [58]: leg = ax.legend(loc='best')

In [59]: for l in leg.legendHandles:            
   ....:     l.set_linewidth(10)
   ....:     

Problem

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.

Original source

Related problems