Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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()

enter image description here

PS. if the line could be made thicker just in the legend, but not in the plot, this would also work.

like image 740
dimka Avatar asked Dec 11 '12 20:12

dimka


People also ask

How do I change the legend text color in Matplotlib?

MatPlotLib with Python To place the legend, use legend() method with location of the legend and store the returned value to set the color of the text. To set the color of the text, use set_color() method with green color. To display the figure, use show() method.

How do you change the color of a legend?

Right-click (Control-click on Mac) the legend and select Edit Colors. Select a data item in the list on the left, and then select a new color in the palette on the right. Click OK.


3 Answers

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]:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

fig = plt.figure()
ax = plt.subplot(111)

colors = []
for i in xrange(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()

the colorful results

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:

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)

leg = ax.legend()

# change the font colors to match the line colors:
for line,text in zip(leg.get_lines(), leg.get_texts()):
    text.set_color(line.get_color())

plt.show()

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

 leg = ax.legend(handlelength=0)
like image 56
DanHickstein Avatar answered Oct 10 '22 05:10

DanHickstein


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)
   ....:     

legend_linewidth.png

like image 9
bmu Avatar answered Oct 10 '22 05:10

bmu


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.

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)

leg = ax.legend()

def color_legend_texts(leg):
    """Color legend texts based on color of corresponding lines"""
    for line, txt in zip(leg.get_lines(), leg.get_texts()):
        txt.set_color(line.get_color())  

color_legend_texts(leg)    

plt.show()

The main difference to note in this answer is that formatting the plot can be completely decoupled from the plotting operation.

like image 8
Patrick Avatar answered Oct 10 '22 05:10

Patrick