macaroni
macaroni

Reputation: 151

Plotting graph in python- LineCollection

Can I simplify lines somehow?

From this:

fig, ax = plt.subplots()

lines = np.array([[[x[i-1], y[i-1]], [x[i], y[i]]]])
line_coll = LineCollection(lines,colors='red')
ax.add_collection(line_coll)
lines = np.array([[[0, R], [x[i], y[i]]]])
line_coll = LineCollection(lines,colors='red')
ax.add_collection(line_coll)


plt.xlim([0, lines[:,:,0].max()])
plt.ylim([0, lines[:,:,1].max()])

to something like this or shorter:

lines = np.array([[[x[i-1], y[i-1]], [x[i], y[i]]]])
lines = np.array([[[0, R], [x[i], y[i]]]])
line_coll = LineCollection(lines,colors='red')
ax.add_collection(line_coll)

I dont want to add still

line_coll = LineCollection(lines,colors='red')
ax.add_collection(line_coll)

for every lines


EDIT:

here is my loop:

while i <= n:
    if i == 0:
        #code
        lines= np.array([[[0, R], [x[i], 0]]])
        #line_coll= LineCollection(lines,colors= 'red')
        #ax.add_collection(line_coll)

    elif i == n:
        #code
        lines = np.array([[[[x[i-1], y[i-1]], [x[i], y[i]]]],
                         [[[0, R], [x[i], y[i]]]]])
        #for line in lines:
        #    line_coll = LineCollection(line, colors='red')
        #    ax.add_collection(line_coll)   

    else: 
        #code
        lines = np.array([[[[x[i-1], y[i-1]], [x[i], y[i]]]],
                         [[[0, R], [x[i], y[i]]]]])
        #for line in lines:
        #    line_coll = LineCollection(line, colors='red')
        #    ax.add_collection(line_coll)
    i= i+1 

#how can i add one loop for all "lines" above?  This below doesnt work good  
for line in lines:
    line_coll = LineCollection(line, colors='red')
    ax.add_collection(line_coll)     

EDIT 2:

while i <= n:
    if i == 0:
        #code
        lines.append(np.array([[[0, R], [x[i], 0]]]))      
    elif i == n:
        #code
        lines.append(np.array([[[x[i-1], y[i-1]], [x[i], y[i]]], [[0, R], [x[i], y[i]]]]))

    else: 
        #code
        lines.append(np.array([[[x[i-1], y[i-1]], [x[i], y[i]]], [[0, R], [x[i], y[i]]]]))

    i= i+1 

for line in lines:
    line_coll = LineCollection(line, colors='red')
    ax.add_collection(line_coll)      

while loop above works (I had to delete some []) good if i set:

plt.xlim([0, 60])
plt.ylim([0, 60])

It doesnt if I set this:

plt.xlim([0, lines[:,:,0].max()])
plt.ylim([0, lines[:,:,1].max()])

There is error list indices must be integers or slices, not tuple for plt.xlim([0, lines[:,:,0].max()])

Upvotes: 1

Views: 253

Answers (1)

Sheldore
Sheldore

Reputation: 39052

You can combine your lines into an array and then loop over the array and add one line artist at a time. The code would look something like

fig, ax = plt.subplots()

lines = np.array([[[[x[i-1], y[i-1]], [x[i], y[i]]]],
                 [[[0, R], [x[i], y[i]]]]])
for line in lines:
    line_coll = LineCollection(line, colors='red')
    ax.add_collection(line_coll)

plt.xlim([0, lines[:,:,0].max()])
plt.ylim([0, lines[:,:,1].max()])

EDIT Try the following trick

lines = []

while i <= n:
    if i == 0:
        #Code here
        lines.append(np.array([[[0, R], [x[i], 0]]]))
    elif i == n:
        #Code here
        lines.append(np.array([[[[x[i-1], y[i-1]], [x[i], y[i]]]],
                         [[[0, R], [x[i], y[i]]]]]))
    else: 
       #Code here
        lines.append(np.array([[[[x[i-1], y[i-1]], [x[i], y[i]]]],
                         [[[0, R], [x[i], y[i]]]]]))
    i= i+1 

for line in lines:
    line_coll = LineCollection(line, colors='red')
    ax.add_collection(line_coll)      

Upvotes: 2

Related Questions