Use annotate by giving list python matplotlib

1.8k views Asked by At

I have a function call draw. I have 2 lists, one with x coordinates an second with y coordinates. I'm making a plot, and I want to write in the plot the x,y coordinates. I see that annotate may be the answer but it can't get a list. I'm trying to do this:

def draw(LuxCoordinates, finalPix, verName, GraphNum):

    xy = (LuxCoordinates,finalPix)
    plt.axes([0.1, 0.1, 0.6, 0.8]);

    plt.xlim(0,3100,);
    plt.ylim(0,100,100);

    plt.xticks(arange(0,3500,350)) #spaces of x axis
    plt.yticks(arange(0,100,10))  #spaces of y axis

    if GraphNum == 1:
        plt.plot(LuxCoordinates, finalPix, 'r', label=verName);
    if GraphNum == 2:
        plt.plot(LuxCoordinates, finalPix, 'g', label=verName);  
    if GraphNum == 3:
        plt.plot(LuxCoordinates, finalPix, 'b', label=verName);    

    legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)    
    plt.scatter(LuxCoordinates, finalPix, linewidths=1)

    **for i, txt in enumerate(xy):
        plt.annotate(txt, (LuxCoordinates[i],finalPix[i]))**
    plt.grid(axis)
    plt.xlabel('Ambient', color='r');
    plt.ylabel('Depth Grows', color='r'); # grayscale color
    plt.title(PngName, color='b');
    savefig(PngName+'_'+date+'_'+currentime+'.png')

But I'm getting this: The plot

And in zoom in: Zoom In You can see it write something, but it's all the list on the first point, I want each x,y will be written near to the point separately, so the first point in the blue graph will be 0,96.6, and in the green it will be 0, 93.54 (you can see the 2 lists)

Can someone please help me with that? Thanks!

1

There are 1 answers

0
jmz On

Your xy has two lists in it to enumerate, not one for each point as you're expecting. Try replacing xy = (LuxCoordinates,finalPix) with xy = zip(LuxCoordinates,finalPix). Alternatively, if you're using xy in its current form elsewhere you could transpose the array in your enumerate call:

for i, txt in enumerate(np.transpose(xy)):