Tuesday, 15 January 2013

plot - How do I assign a legend to some data within a for loop in Python? -



plot - How do I assign a legend to some data within a for loop in Python? -

i have python code plots figure. figure contains 5 curves. each curve made of 5 points. want label these curves using legend.

lets 5 curves have next marker, colour , label designations:

markers = ['o','s','p','h','d'] colours = ['b','g','r','y','k'] labels = ['a','b','c','d','e']

just clarity first curve made of 5 "o" points, points bluish , labelled "a".

my info list of lists so:

curve1 = [1,2,3,4,5] curve2 = [2,3,4,5,6] curve3 = [3,4,5,6,7] curve4 = [4,5,6,7,8] curve5 = [5,6,7,8,9] xaxis = [1,2,3,4,5] info = [curve1,curve2,curve3,curve4,curve5]

now plot 5 curves so:

for j in range(len(xaxis)): in range(len(xaxis)): pylab.plot(xaxis,data[i],color=colours[i],marker=markers[i],label=labels[i]) pylab.legend(loc=0) pylab.show()

note there loop within loop causes legend print out each label 5 times. how can around ?

you need add together custom label each:

for i,curve in enumerate(data): pylab.plot(xaxis,curve,color=colours[i],marker=markers[i],label=labels[i]) pylab.legend(loc=0) pylab.show()

this should give:

python plot legend

No comments:

Post a Comment