Manolete Manolete - 24 days ago 4
Python Question

Printing labels in a loop

I want to loop and plot file information

-1 1 0.732313
-1 2 1.33585
-1 4 1.05306
-1 8 1.56261
-1 16 1.90336
-1 32 1.71105
-1 64 1.8319


which is loaded to
mat0

This is what I have so far:

mat0 = genfromtxt("mydata")
fig1 = plt.figure()
ax = fig1.add_subplot(111)
mybel =-1
count =0
while (count < 60):
i=count
j= i+6
plt.plot(mat0[i:j,1], mat0[i:j,2],label="Size %s"%mybel)
count = count + 7
mybel = mybel +1

plt.show()


The problem is that I do not get labels printed at all. I do not get any error either. What am I missing?

Answer

You need to call the legend object. You do this by calling plt.legend().

mat0 = genfromtxt("mydata")
fig1 = plt.figure()
ax = fig1.add_subplot(111)
mybel =-1
count =0
while (count < 60):
    i=count
    j= i+6                                      
    plt.plot(mat0[i:j,1], mat0[i:j,2],label="Size %s"%mybel)
    count = count + 7
    mybel = mybel +1
plt.legend()
plt.show()
Comments