From: Justin McCann <jneilm@gm...>  20110713 19:32:40

2011/7/13 SULSEUNGJIN <sulsj0270@...>: > Hi, > > I'm plotting thousands of short lines on a plot. Because "plot" and "Line2D" > are quite slow for this case, I'm trying to use lineCollection. Here comes > the part of my testing code: > > ... > segs = [] > > # Manual set for testing > x2 = np.zeros(2,dtype=int) > ys2 = [np.zeros(2,dtype=float)] > x2[0] = 1000000 > x2[1] = 2000000 > ys2[0][0] = ys2[0][1] = 50 > segs.extend( [zip(x2,y) for y in ys2] ) > > for i in range(0, len(records)): > ... > # get xStart, xEnd, and y > ... > > x2[0] = xStart > x2[1] = xEnd > ys2[0][0] = ys2[0][1] = y > segs.extend( [zip(x2,y) for y in ys2] ) > > line_segments = LineCollection(segs, linewidth=4, alpha=0.3, colors = > 'r', linestyle = 'solid') > ax.add_collection(line_segments) > ... > > The problem I have is only the first segment of the lines in the "segs" > which I manually set the values is shown in the plot. If I move the first > part in the for loop to test, it works. What am I missing? > > Thank you in advance. Are you sure it's the first segment that shows up, and not the last one? You're reusing the numpy array x2 and the list ys2. You're overwriting the values inside them each time through the for loop, but segs just contains len(records) references to the exact same arrays in memory (x2 and ys2). The reason it works if you move the first part inside is that it creates a new numpy.array x2 and a new python list ys2 each time then, which is what you want. Justin 