繁体   English   中英

Python:从 x 轴上的点 (X1,0) 到点 (X2,Y2) 绘制线的问题

[英]Python: Problem with plotting lines from points (X1,0) on x axis to points (X2,Y2)

我在 X 轴上有点,我想从每个点到另一个点(X2,Y2)做直线。

此代码适用于从 Y=35 到 X 点( P )的蓝线,并且它有效

pos_fixed = np.array([0, TR])
lines = np.array([[[pos, 0], pos_fixed] for pos in P])
line_coll = LineCollection(lines)

fig, ax = plt.subplots()
ax.add_collection(line_coll)

plt.xlim([0, lines[:,:,0].max()])
plt.ylim([0, lines[:,:,1].max()])
plt.xlabel('Oś')
plt.ylabel('Promień')
plt.show()

这里是应该做我在开头描述的代码:


for s in range(len(P)-1):
    position = np.array([P[s], 0])
    lines = np.array([[position, [x2[s], y2[s]]]])
    line_coll = LineCollection(lines)
    fig, ax = plt.subplots()
    ax.add_collection(line_coll)
    plt.xlim([0, lines[:,:,0].max()])
    plt.ylim([0, lines[:,:,1].max()])
plt.show()

我的期望是在附图上(我有红色和紫色的点,我不知道如何做绿线)。

这段代码(第二个)显示了几十个图表(每条绿线分开),不包括(我希望)上一个代码/上一个图表。

图片

您正在每个循环上创建一个图形。 您可以先设置图形,然后在for循环中添加行。 像这样重新排列你的代码:

# create the figure
fig, ax = plt.subplots()
plt.xlim([0, lines[:,:,0].max()])
plt.ylim([0, lines[:,:,1].max()])

# lines from the first part of the question
pos_fixed = np.array([0, TR])
lines = np.array([[[pos, 0], pos_fixed] for pos in P])
line_coll = LineCollection(lines,colors='blue')
ax.add_collection(line_coll)
plt.xlabel('Oś')
plt.ylabel('Promień')


# lines for the second part
for s in range(len(P)-1):
    position = np.array([P[s], 0])
    lines = np.array([[position, [x2[s], y2[s]]]])
    line_coll = LineCollection(lines,colors='green')
    ax.add_collection(line_coll)

plt.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM