Python 绘制点与点之间的线
我想画两点之间的线,我的点在不同的列中定义Python 绘制点与点之间的线,python,matplotlib,Python,Matplotlib,我想画两点之间的线,我的点在不同的列中定义 #coordinates of the points #point1(A[0],B[0]) #point2(C[0],D[0]) #line between point1 and point 2 #next line would be #point3(A[1],B[1]) #point4(C[1],D[1]) #line between point3 and point 4 plot_result: A B C D E F
#coordinates of the points
#point1(A[0],B[0])
#point2(C[0],D[0])
#line between point1 and point 2
#next line would be
#point3(A[1],B[1])
#point4(C[1],D[1])
#line between point3 and point 4
plot_result:
A B C D E F
0 0 4 7 1 5 1
1 2 5 8 3 3 1
2 3 4 9 5 6 1
3 4 5 4 7 9 4
4 6 5 2 1 2 7
5 1 4 3 0 4 7
我尝试使用以下代码:
import numpy as np
import matplotlib.pyplot as plt
for i in range(0, len(plot_result.A), 1):
plt.plot(plot_result.A[i]:plot_result.B[i], plot_result.C[i]:plot_result.D[i], 'ro-')
plt.show()
但这是一种无效的语法。我不知道如何实现这个方法图的前两个参数是x和y,可以是单点或类似阵列的对象。如果要绘制从点(x1,y1)到点(x2,y2)的直线,必须执行以下操作:
for plot_result in plot_result.values: # if plot_results is a DataFrame
x1 = row[0] # A[i]
y1 = row[1] # B[i]
x2 = row[2] # C[i]
y2 = row[3] # D[i]
plt.plot([x1,x2],[y1,y2]) # plot one line for every row in the DataFrame.
在matplotlib中,两点
(x1,y1)
和(x2,y2)
之间的直线通过plt([x1,x2],[y1,y2])
创建。