matplotlib - In python, drawing a list of line segments given their indices -


given list of vertices, , list of line segments (indices refer vertices of segment), what's best way of drawing lines segments may not form continuous line?

i can way, it's clunky; there better way?

vertices=[[0,0],[1,1],[1,2],[4,1]] segs=[[0,1],[0,2],[2,3]]   seg in segs:     x = vertices[seg[0]][0], vertices[seg[1]][0]     y = vertices[seg[0]][1], vertices[seg[1]][1]     plot(x, y, 'k') 

this get

@cactuswoman gives way works. full code.

import matplotlib import matplotlib.collections import matplotlib.pyplot  vertices=[[0,0],[1,1],[1,2],[4,1]] segs=[[0,1],[0,2],[2,3]]   lines = [[tuple(vertices[j]) j in i]for in segs] lc = matplotlib.collections.linecollection(lines)  fig, ax = matplotlib.pyplot.subplots() ax.add_collection(lc) matplotlib.pyplot.xlim([0,4]) matplotlib.pyplot.ylim([0,2]) matplotlib.pyplot.show() 

you want linecollection this. give list of lists of tuples, each tuple vertex , each list contains vertices of segment.

lines = [[tuple(vertices[j]) j in i]for in segs]  lc = matplotlib.collections.linecollection(lines) 

then use add_collection add axes.


Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -