在matplotlib中的对数螺线上的两点之间绘制一段

时间:2018-02-05 07:41:17

标签: python matplotlib

我绘制了一个算法螺旋,我在其上标记列表numbers中的点,这表示距螺旋开始的距离(绿点)。然后我尝试在两个选定点之间绘制一个段。蓝点属于scatter函数绘制的一组点,黑点仅通过其坐标直接选择。我的代码:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(9,9))
ax = fig.add_subplot(111)   
a = 1.1
b = 0.1
th = np.linspace(0, 20, 1000)
x = a*np.exp(b*th)*np.cos(th)
y = a*np.exp(b*th)*np.sin(th)

diffs = np.sqrt(np.diff(x)**2+np.diff(y)**2)
length = diffs.sum()

numbers = [2,4,7,8,13,16,18,19,22,26,28,31,35,37,44,48,55,56,59,60,65]
p2 = []
for i in range(len(numbers)):
    cumlenth = np.cumsum(diffs)
    s = np.abs(np.diff(np.sign(cumlenth-numbers[i]))).astype(bool)
    c = np.argwhere(s)[0][0]
    p = [x[c]], [y[c]]
    p2.append(list(p))

ax.cla()
ax = fig.add_subplot(111)

for j in range(len(p2)):   
    ax.scatter(p2[j][0],p2[j][1], color='green')

ax.scatter(p2[10][0],p2[10][1], color='blue')
ax.scatter(p2[20][0],p2[20][1], color='blue')

ax.plot(x, y)

plt.plot(p2[10], p2[20], color='k',marker='o')

plt.show()

我收到的地方:

enter image description here

为什么蓝点和黑点不重叠?

1 个答案:

答案 0 :(得分:1)

x[1,3], y[2,4]

情节(x,y) - > 情节(1,3,2,4)
如果要在两个点(x,y)之间用绘图绘制一个段,则应更新x和y: x [1,2],y [3,4]

使用您的代码,您可以尝试:

a1 = [p2[10][0],p2[20][0]]
a2 = [p2[10][1],p2[20][1]]
plt.plot(a1,a2,color='k',marker='o' )
相关问题