[英]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.