imsc
imsc

Reputation: 7840

Splitting the legend in matploblib

Is it possible to split up a single big legend into multiple (usually 2) smaller ones.

from pylab import *

t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)
plot(t, s, linewidth=1.0, label="Graph1")
grid(True)
s = sin(4*pi*t)
plot(t, s, color='r',linewidth=1.0, label="Graph2")

legend(loc='lower left')
show() 

I would like to split the legend into two and place them where white space is available.

Upvotes: 8

Views: 12387

Answers (1)

imsc
imsc

Reputation: 7840

I got the answer from http://matplotlib.sourceforge.net/users/plotting/examples/simple_legend02.py

from pylab import *

t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)
p1, = plot(t, s, linewidth=1.0, label="Graph1")
grid(True)
s = sin(4*pi*t)
p2, = plot(t, s, color='r',linewidth=1.0, label="Graph2")

l1 = legend([p1], ["Graph1"], loc=1)
l2 = legend([p2], ["Graph2"], loc=4)
gca().add_artist(l1)

show() 

Here, the only drawback is I have to give the labelname twice.

enter image description here

Upvotes: 7

Related Questions