Thanks, Justin

I think I made a confusing example code. Here comes new one:

segs = []

for i in range(0, len(vec)):

```
x1 = vec[i][0]
#x1 = 3000000
x2 = vec[i][1]
#x2 = 4000000
y1 = y2 = vec[i][2]
segs.extend( [[(x1,y1),(x2,y2)]] )
```

line_segments = LineCollection(segs, linewidth=3, alpha=0.3, colors = ‘r’, linestyle = ‘solid’) ax.add_collection(line_segments)

The above code doesn’t show any lines. But if I comment out one or both of commented parts, the lines are shown.

I mean the below cases are all work.

```
x1 = vec[i][0]
x2 = 4000000
y1 = y2 = vec[i][2]
```

or

```
x1 = 3000000
x2 = vec[i][1]
y1 = y2 = vec[i][2]
```

I think it’s wierd. I’m pretty sure that the 2D array has valid values.

Jin

## ···

Date: Wed, 13 Jul 2011 15:32:33 -0400

Subject: Re: [Matplotlib-users] Question on LineCollection

From: jneilm@…287…

To: sulsj0270@…32…

CC: matplotlib-users@lists.sourceforge.net

2011/7/13 SULSEUNG-JIN <sulsj0270@…32…>:

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