Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 670
imsc Avatar asked Oct 08 '22 06:10

imsc


1 Answers

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

like image 94
imsc Avatar answered Oct 13 '22 03:10

imsc