Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

matplotlib - Plotting lines connecting points

I know there is another very similar question, but I could not extract the information I need from it.

plotting lines in pairs

I have 4 points in the (x,y) plane: x=[x1,x2,x3,x4] and y=[y1,y2,y3,y4]

x=[-1 ,0.5 ,1,-0.5]
y=[ 0.5,  1, -0.5, -1]

Now, I can plot the four points by doing:

import matplotlib.pyplot as plt

plt.plot(x,y, 'ro')
plt.axis('equal')
plt.show()

But, apart from the four points, I would like to have 2 lines:

1) one connecting (x1,y1) with (x2,y2) and 2) the second one connecting (x3,y3) with (x4,y4).

This is a simple toy example. In the real case I have 2N points in the plane.

How can I get the desired output: for points with two connecting lines ?

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you're going to need separate lines for each segment:

import numpy as np
import matplotlib.pyplot as plt

x, y = np.random.random(size=(2,10))

for i in range(0, len(x), 2):
    plt.plot(x[i:i+2], y[i:i+2], 'ro-')

plt.show()

(The numpy import is just to set up some random 2x10 sample data)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...